Let me explain detailed; I have notification and this notification opens B activity
with two cases.
Cases :
Case-1
I click to the notification and it opens the B activity
with case-1
. When i press back
i want to go to the A activity
and kill B activity
. I dont need B activity
anymore. Everything easy from here without using flags
. When I'm on B activity
and press back two times from here, it goes A activity
and then closes the app
. My trouble here is, if i open the app
from navigation buttons of phone (can't remember the name of this button) app is opening from B activity
. That's not what i expected. I want to open A activity
. Don't want to see B activity
anymore.
Case-2
I click to the notification and it opens the B activity
with case-2
.When i press back
i want to bring A activity
to the front, without creating anything new. If i press back on B activity
, two times and close the app and then again re-open app from navigation button of phone, want to open app from A activity
.
So how can i make this correctly, i tried to use flags
(i already read docs) but couldn't get work.
What flags
should i use when i open the B activity
and onBackPress
method of B activity
to go A activity
as i wanted
In your finish() method, you want to use isActivityVisible() to check if the activity is visible or not. There you can also check if the user has selected an option or not. Continue when both conditions are met.
flag — FLAG_ACTIVITY_CLEAR_TASK: This clears any existing task that would be associated with the Activity before the Activity is started. This Activity then becomes the new root of the task and old Activities are finished. It can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
If the user presses or gestures Back, the current activity is popped from the stack and destroyed. The previous activity in the stack is resumed. When an activity is destroyed, the system does not retain the activity's state.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
This should be achievable by adding
android:launchMode="singleTask"
to the A activity in the Manifest, then you can just open A activity from B activity onBackPressed and you will have A only once in the stack.
If it's not working the way you want, you can create an abstract class that extends Activity and handle the stack in a static object, then A & B must extend this new class
try this
Intent intent = new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With