Use startActivityForResult from non-activity in Android

Use startActivityForResult from non-activity in Android

In Android, startActivityForResult is typically used within an Activity to start another Activity and receive a result from it. If you need to use startActivityForResult from a non-Activity class, you can do so by passing a reference to the calling Activity to the non-Activity class and then calling startActivityForResult from that Activity. Here's how you can achieve this:

  1. Pass the Activity Reference to the Non-Activity Class:

    In your non-Activity class, create a constructor or method that accepts a reference to the calling Activity. This reference will be used to call startActivityForResult. For example:

    public class NonActivityClass { private final Activity callingActivity; public NonActivityClass(Activity activity) { this.callingActivity = activity; } public void startAnotherActivityForResult() { Intent intent = new Intent(callingActivity, AnotherActivity.class); callingActivity.startActivityForResult(intent, requestCode); } } 
  2. Call startAnotherActivityForResult from the Activity:

    In your Activity, create an instance of the non-Activity class, passing this (the reference to the Activity) to its constructor. Then, call the method that starts another Activity for a result. For example:

    public class MyActivity extends Activity { private NonActivityClass nonActivityClass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); // Initialize the non-Activity class with a reference to this Activity nonActivityClass = new NonActivityClass(this); } public void someMethod() { // Call the non-Activity class method to start another Activity for a result nonActivityClass.startAnotherActivityForResult(); } // Handle the result in onActivityResult @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == requestCode) { // Handle the result here if (resultCode == RESULT_OK) { // Process the result data } else { // Handle the case where the result is not OK } } } } 

In this example:

  • The NonActivityClass accepts a reference to the calling Activity in its constructor.
  • The non-Activity class uses this reference to call startActivityForResult.
  • In the MyActivity class, an instance of NonActivityClass is created and initialized with a reference to the activity (this).
  • The someMethod in MyActivity can then be used to start another Activity for a result using startAnotherActivityForResult.
  • The result is handled in the onActivityResult method of the MyActivity class.

This approach allows you to use startActivityForResult from a non-Activity class by passing the necessary context to it.


More Tags

crashlytics oauth-2.0 xlsxwriter build-automation spock openurl searchbar angularjs-ng-repeat svnignore cultureinfo

More Java Questions

More Everyday Utility Calculators

More Financial Calculators

More Investment Calculators

More Fitness Calculators