Arrow Cen
Arrow Cen

Reputation: 753

call startActivityForResult from another activity

I have an Activity A which calls startActivityForResult to start Activity B, normally user can click a button from Activity A to start Activity B for result.

Now I'm need to open Activity B from Activity C, when Activity B returns, it should go back to Activity A. Is this possible?

I'm trying to use an intent to start Activity A from Activity C and call A's startActivityForResult method, but it seems impossible to get the instance of Activity A.

The normal stack is A->B but now I want it to be C->B, but when B returns, it return result to A.

I'm thinking if there is a possible way to do C->A->B but hide A from user?

Upvotes: 3

Views: 2519

Answers (3)

Amer Dajah
Amer Dajah

Reputation: 121

just add getActivity() before start for result

getActivity().startActivityForResult(intent, 1);

Upvotes: 1

Sahar Avr
Sahar Avr

Reputation: 1168

This is a very clever question. I think Kevin's answer is the right way to go, although I do want to add about the subject:

First we need to understand the Activity's launch mode. In android there are 3 activity launch modes:

  1. Standard Mode: This is the default mode, where a new Activity will always be created to work separately with each Intent sent.

  2. Single Top Mode: This mode acts almost the same as the standard mode, only difference is if there already is an Activity instance with the same type at the top of stack in the caller Task, a new Activity will not be created (an Intent will be sent to an existing Activity through onNewIntent() method).

  3. Single Task Mode: This mode is quite different from standard and singleTop. An Activity with singleTask launchMode is allowed to have only one instance.

Basically, we can assign a launchMode directly as an attribute inside the AndroidManifest.xml:

<activity
        android:name=".SingleTaskActivity"
        android:label="singleTask launchMode"
        android:launchMode="singleTask">
</activity>


As for your question, assuming we stick with the Standard laucnhMode, the only workaround I can think of to achieve what you want, will be to call the finish() method after each call to startActivity() which is not ActivityA. For example, supposing I'm inside ActivityC:

public void buttonClick(View view) {

        switch (((Button) view).getText().toString()) {
            case "A":
                startActivity(new Intent(getApplicationContext(), ActivityA.class));
                break;

            case "B":
                startActivity(new Intent(getApplicationContext(), ActivityB.class));
                finish();
                break;

            case "C":
                startActivity(new Intent(getApplicationContext(), ActivityC.class));
                finish();
                break;
        }
    }

This way, no matter where I hit "back", I will always return to ActivityA. I understand this may not solve your problem, but I hope that it does leave you with a few more ways of thinking.

Let me know if this was of any help for you at all, if not simply comment and I'll edit my post.

Upvotes: 2

Kevin Krumwiede
Kevin Krumwiede

Reputation: 10308

So your stack is A-C-B, and you want it to just be A? This is as simple as calling finish() in C's onActivityResult(...) when it receives a result from B. This could be any result, or some particular result. And it could return this result to A.

You can't reliably get the instance of Activity A, because when A is in the background or backstack, there's no guarantee that there is any instance to get. It could have been persisted and destroyed.

Upvotes: 1

Related Questions