Reputation: 1027
I have the following activity path:
Main -> A0 -> A1 -> A2 -> B
or
Main -> B
On Main
, the user can select to either show something on B
, or to create something new with the A
series.
After completing the A
series, it goes to B
.
When the user gets to B
via the A
route, I want the back button to go to Main
. But when the user is in the A
series, I want the back button to go to the previous A
(or Main
from the first A
).
I tried using FLAG_ACTIVITY_NO_HISTORY
when I create the intents, but that just makes everything go back to Main
.
Is there a way to mark the activities for removal once I hit a threshold?
Upvotes: 1
Views: 47
Reputation: 305
You can fire a new intent for Main activity with SINGLE_TOP and CLEAR_TOP flags in onBackPressed of B. So no matter how you landed at B, pressing back takes you to Main with all history cleared. No need to mess with A series activities.
Upvotes: 0
Reputation: 12181
You could add a BroadcastReceiver
in all activities you want to close (A0, A1, A2):
public class MyActivity extends Activity {
private FinishReceiver finishReceiver;
private static final String ACTION_FINISH =
"com.mypackage.MyActivity.ACTION_FINISH";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finishReceiver= new FinishReceiver();
registerReceiver(finishReceiver, new IntentFilter(ACTION_FINISH));
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(finishReceiver);
}
private final class FinishReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_FINISH))
finish();
}
}
}
To close these activities trigger the following broadcast from Activity B.
sendBroadcast(new Intent(ACTION_FINISH));
Here is the github example project for the same.
Upvotes: 2
Reputation:
Define the logic in this very way
In Series of Activity A define the logic in this very way
public static Activity A=null;
//in onCreate();
A=this;
//Same way in B
public static Activity B=null;
B=this
now wherever you want the back condition to be checked override the OnbackPressed.
@Override
public void onBackPressed()
{
if(Activity_A.A!=null)
{
finish();
return;
}
if(Activity_B.B!=null){
//your condition
finish();
}
}
Upvotes: 0