Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bring activity front if it is running or create new one with backpress

Let me explain detailed; I have notification and this notification opens B activity with two cases.

Cases :

  1. If app is closed. (not running on background)
  2. If app is opened. (on background or front)

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

like image 338
Farukest Avatar asked Nov 09 '17 15:11

Farukest


People also ask

How do you know if activity is visible or not?

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.

What is true about flag activity new task?

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.

What happens to the back stack when you switch between activities?

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.

How would you send data back to a previous activity in Java?

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.


2 Answers

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

like image 172
Quentin Menini Avatar answered Sep 29 '22 04:09

Quentin Menini


try this

 Intent intent = new Intent(context, YourActivity.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
 startActivity(intent);
like image 40
Naimish Vinchhi Avatar answered Sep 29 '22 04:09

Naimish Vinchhi