Pregunta

I'm developing an application in which I have a MainActivity, on that I have a button which calls Activity1(the theme of this activity is set to dialog i.e. android:theme="@android:style/Theme.Holo.Light.Dialog), so I can't finish the MainActivity, otherwise Activity1 will be on top of home screen.

On my Activity1, my other activity i.e. Activity2 is called. This is also same as Activity1 with dialog theme. Now from Activity2 I have called Activity3 which is normal activity without dialog theme.

Now, I want to finish my MainActivity from Activity2.

How can I achieve that?

Any kind of help will be appreciated.

¿Fue útil?

Solución

You can start each activity like startActivityForResult() and check result of activity. So, if you want to close first activity, you should set specific result of each activity and call finish(). In each activity, you check result in onActivityResult(), so if you see your specified result, you should close this activity (or set result if it not first activity too)

In first activity, I want to start second

Intent i = new Intent(Activity1.this, Activity2.class);
startActivityForResult(i, 0);

In second activity, I want to start third

Intent i = new Intent(Activity2.this, Activity3.class);
startActivityForResult(i, 0);

So, In third activity I want to start fourth and close each other

Intent i = new Intent(Activity3.this, Activity4.class);
startActivity(i);

setResult(RESULT_OK);
finish();

And in each activity (1, 2) I should check method onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        setResult(RESULT_OK);
        finish();
    }
}

Otros consejos

do you want to close Activity2, Activity1, and then MainActivity ?

if so, you can use the startActivityForResult and onActivityResult for each of the activities, and check if the returned result is the one that requests to close the current activity and pass it further.

if not, such a thing can be done using a service that will be bound between the activities.

From what I understand, you want to finish MainActivity when Activity 3 is started. If this is the case and it is ok to clear the other two Activities as well, you can us an Intent flag

Intent intent (Activity2.this, Acitivity3.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

If I understand you correctly this will accomplish what you want. Activity 3 will be started while clearing all other Activities from the stack.

Docs

I think its not a working flow android system would expect to occur thats why you cant shut down the whole app with one command. What you can do is to use the startActivityForResult and check the source, the status and close the activities in cascade.

Heres an example: How to manage `startActivityForResult` on Android?

You can create specific codes for each activity that start the next one.

While starting an activity, send your activity to new one as an extra. For example:

in main:

  Intent in= new Intent("your action");
  in.putExtra("preAct",this);
  startActivity(in);

Then use your preAct to finish it.

in new activity:

 Activity prev = (Activity)this.getIntent().getSerializableExtra("preAct");

then all you need

 prev.finish()

to finish it whenever you want

Check noHistory attribute of activity. If You set it to true, user won't be able to get back to this activity after he leaves it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top