Destroy activity when creating the same activity - java

I have 2 activities A and B, A is defined as "singletask" on the manifest launch mode, as it should be and i cannot change this. A launches activity B, then when B clicks the back button i want to launch activity A again, but i want to destroy the previous activity, and create a new instance of A. right now the following code is not working. It just takes me back to the old A activity.
<activity
android:name="A"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="#style/AppTheme" />
public void onBackPressed() {
Intent intent = new Intent(this,A.class);
intent.putExtra("newextra1","newextra1");
intent.putExtra("newextra2","newextra2");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}

Just call finish() in Activity A after you call the intent for Activity B. It will be executed even if another Intent is started before calling finish().
You are calling finish() in Activity B but not Activity A.
if you want the old activity in some cases and new in some, then just pass a flag(boolean) in intent that tells the activity to restart or not. Then if the flag is yes you do
finish();
startActivity(getIntent());
in Activity A.

Try:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Related

How to detect if my app is called by another

I have two entrnces to my app: from menu and using Intent.
In the second case I need to return some EXTRA_OUTPUT by
setResult(Activity.RESULT_OK, myIntentWithData)
finish()
so I need to detect if my app was called by another(finish in that case or continue work otherwise)
If your activity was called using startActivityForResult(), you can use getCallingActivity() method in your Activity. It will return a ComponentName of the activity that started yours (or null if your activity wasn't started with startActivityForResult() method). Then you can get getCallingActivity().getPackageName() or getCallingActivity().getClassName().
More info here getCallingActivity, https://stackoverflow.com/a/5336612/3569545
Use getIntent() method to get the intent that was the reason to start your activity.
Then if it was launched from the apps menu the intent will have action MAIN.
val intent = getIntent();
if (intent.getAction() != "android.intent.action.MAIN") {
setResult(Activity.RESULT_OK, myIntentWithData)
finish()
}

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 :
If app is closed. (not running on background)
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
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);
try this
android:launchMode="singleTask" in android manifest file
You can achive this by adding FLAG_ACTIVITY_REORDER_TO_FRONT
Intent i = new Intent(context, Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
You May try this isTaskRoot() Which will return B is root
if it is true then launch A
other wise you may finish B
B Activity
#Override
public void onBackPressed() {
if (isTaskRoot()) {
//call A which is not exist
Intent i =new Intent(B.this,A.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}else {
//Finish B if A Already Exixt
finish();
}
}
You can call B Activity on Notification Click
if A is present then u can finish B else you can launch A
If set FLAG_ACTIVITY_CLEAR_TOP, the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
Just put this in ActivityB onBackPressed:
Intent i = new Intent(ActivityB.this , ActivityA.calss);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
finish();
How solve case 1:
finish(); on ActivityB BackPressed method make ActivityB finish after open ActivityA. So after opens ActivtyA, ActivityB will shut down.
How solve case 2:
With this combination flag, It will do what you want. It will close all activities in stack and just keep destination activity. If instance of activity exist it will use it and calls OnNewInstance and if not it will creates new one.
If this is the only instance of Activity B being used, you can add the flag noHistory to the manifest for Activity B
android:noHistory="true"
This will stop Activity B being added to the back stack, this is also possible dynamically by using the Intent Flag FLAG_ACTIVITY_NO_HISTORY when calling Activity B.
As for having Activity A start when Activity B is killed #Quentin Menini's answer of having a single task activity set in the manifest will work if that is the only way you wish Activity A to be accessed, or the Intent Flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT as #Naimish Vinchhi has suggested, will have the desired effect in this instance.
https://developer.android.com/reference/android/content/Intent.html
link to see all possible Intent flags
https://developer.android.com/guide/topics/manifest/activity-element.html
link for all possible manifest activity options

Create new Instance Of activity by Intent every time android?

How to prevent to create every time Instance of Store activity?
When I call startActivity(new Intent(this,StoreActivity.class)), it will create new instance and call OnCreate Method in StoreActivity. I want to call one time Oncreate.
Is this Possible ?
Do this
startActivity(new Intent(
this,StoreActivity.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));
From Android Documentation
public static final int FLAG_ACTIVITY_SINGLE_TOP
If set, the activity will not be launched if it is already running at the top of the history stack.
Constant Value: 536870912 (0x20000000)
Start your activity like this:
Intent storeIntent = new Intent(this, StoreActivity.class);
storeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(storeIntent);
It will call onCreate() only once on first launch of activity, if activity is already running it calls only onNewIntent() instead of create new instance of activity or calling onCreate.
This is not possible since each time you press back button, onBackPressed() method is called which actually destroys your StoreActivity.
If you want this method to not destroy your activity, just remove or comment out super.onBackPressed() line in this method.
In this case your activity will not be destroyed when back button is pressed, but then you will have to use any other logic to bring your MainActivity to top of the stack.
try to finish the first activity after starting the new one,
add this code after the Intent command.
finish();
like that:
startActivity(new Intent(this, newActivity.class));
finish();
check this link
Finish an activity from another activity

using intent flags and androidmanifest

I have a service that starts an activity (say Activity A) as follows:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
getApplicationContext().startActivity(intent);
when I start my app, Activity A appears instead before the app's first activity.
I had 2 solutions to work this around:
1)
changing the above code to:
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
problem:
after launching the activity and pressing home-key, I still see the activity in the history stack (=recent tasks). how come?
after I launch my app, the two tasks merge into one with the app's first activity at the top. but any other app, create a new task and the task of Activity A remains. how come?
2)
changing the above code to:
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
and adding to the manifest:
<activity
android:name="org.achartengine.GraphicalActivity"
android:taskAffinity="org.achartengine"
>
where Activity A = android:name="org.achartengine.GraphicalActivity"
problem:
I keep unnecessary task stack.
I assume few people start that activity from the service. And even then they will do it only once. Meaning it would be more neat to clean the activity from any history stack when the user clicks home/back key.
which of the options is preferred? (or another one?)
A quick fix is adding android:launchMode="singleTop" to the activity in the manifest, that way there will only be one and not multiple instances. The other way to accomplish the same thing is to use the following just prior to starting the intent.
Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
There are a couple of other possibilities to try in the manifest put
android:noHistory="true"
and make sure your are calling
finish();
to end that intent.
for the activity or a combination of these 2 intent flags
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP
If the above doesn't work, the last thing I can think of is
moveTaskToBack(true);
which doesn't solve the problem.
Hope this helps.
This is a method that will keep the first activity off your stack.
Thread nohistory = new Thread () {
public void run () {
try {
Intent i = new Intent(Splash.this, ActivitySplash.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
nohistory.start();

Finishing Activity from another activity [duplicate]

This question already has answers here:
close an activity from other activity?
(5 answers)
Closed 9 years ago.
Say I have 3 activities A, B and C. A leads to B which leads to C. I would like to be able to move back and forth between A and B but I want to finish both A and B once C gets started. I understand how to close B when starting C via the intent but how do I also close A when C gets started?
Use this flag when you are opening the C acitivity.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
This will clear all the activities on top of C.
Since A is your root (starting) activity, consider using A as a dispatcher. When you want to launch C and finish all other activities before (under) it, do this:
// Launch ActivityA (our dispatcher)
Intent intent = new Intent(this, ActivityA.class);
// Setting CLEAR_TOP ensures that all other activities on top of ActivityA will be finished
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add an extra telling ActivityA that it should launch ActivityC
intent.putExtra("startActivityC", true);
startActivity(intent);
in ActivityA.onCreate() do this:
super.onCreate();
Intent intent = getIntent();
if (intent.hasExtra("startActivityC")) {
// Need to start ActivityC from here
startActivity(new Intent(this, ActivityC.class));
// Finish this activity so C is the only one in the task
finish();
// Return so no further code gets executed in onCreate()
return;
}
The idea here is that you launch ActivityA (your dispatcher) using FLAG_ACTIVITY_CLEAR_TOP so that it is the only activity in the task and you tell it what activity you want it to launch. It will then launch that activity and finish itself. This will leave you with only ActivityC in the stack.

Categories

Resources