Create new Instance Of activity by Intent every time android? - java

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

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

When bringing Android activities from the stack to the front, how do I refresh (reinitialize) them? So to run onCreate again etc

If I'm bringing Android activities from the stack to the front, how do I refresh them? So to run onCreate again etc.
My code below, in conjunction with setting activities in the Android manifest to android:launchMode="singleTask" allows me to initiate an activity if that activity is not already active within the stack, if it is active within the stack it is then brought to the front.
How do I then, if the activity is brought to the front refresh it so that onCreate is ran again etc.
Intent intent = new Intent(myActivity.this,
myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
I think FLAG_ACTIVITY_CLEAR_TOP will resolved your problem:
Intent intent = new Intent(myActivity.this, myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I don't think there is an explicit way to refresh onCreate, perhaps you may want to add the code you want reloaded into onResume.
This workaround may work if you want to keep your code in onCreate.
finish();
startActivity(getIntent());
If all you want to do is refresh the content, you should override the onResume method, and add in the code to perform the refresh in this method. To do this, use the following code within the activity that you want to perform the refresh, (ie, not the same activity that you are calling startActivity() from):
#Override
protected void onResume(){
super.onResume();
//add your code to refresh the content
}
Tip: If you are using Android Studio, press Alt+Insert (while you have the Java file open), then click Override Methods, find onResume, and it should provide you with a basic template for the method.
The diagram I added shows the order that the methods are run (this is known as the Activity Lifecycle). onCreate() is run whenever an Activity is first created, followed by onStart(), followed by onResume(). As you can see, when a user returns to an Activity, onCreate() is not run again. Instead, onResume() is the first method that is called. Therefore, by putting your code into the onResume() method, it will be run when the user returns to the activity. (Unlike onCreate(), which will not be run again).
Extra info: Since you will be initially setting the data in onCreate() and then refreshing it within onResume(), you might want to consider moving all of your code used to initially set the data to onResume() as well. This will prevent redundancy.
Edit: Based on your following comment, I can give the following solution:
I'm wanting to properly refresh the page, e.g. if there is a variable count initialised at 0. And though running the activity it's has became equal to 300. When the activity is called (intent) then refreshed, count will once again be equal to it's initial value. Do you know how to do this?
Without your current activity's code, there is not much to work with, but here is some pseudo-code as to how I would accomplish your problem:
public class MyActivity extends Activity {
TextView numberTextView;
int numberToDisplay;
#Override
protected void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(myContent);
numberTextView = (TextView) findViewById(R.id.numberTextView);
numberTextView.setText(numberToDisplay+"")//converts the integer to a string
}
#Override
protected void onResume(){
super.onResume();
numberToDisplay = 0;
numberTextView.setText(numberToDisplay+"");
}
}

using startActivityForResult() and then finish()

I have a view in my android app that needs to open another view
and also close\finish all the calling hierarchy that made it open.
How can i do this?
If i use this code, the finish() is never called no?
Intent intent = new Intent(A.this, B.class);
setResult(RESULT_OK);
startActivity(intent);
finish();
You can clear whole stack using:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
this is a solution if you want to start new activity, and close all activities that were open before it.
Google has an informative article on the Android back stack here.
In your code the .finish() is called and your calling activity should finish, but only that Activity and not others in the back stack. You can maintain some control of your back stack via Intent flags that are explained in the article, i.e.FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_SINGLE_TOP.
Another method is to use startActivityForResult(...) and close unwanted Activities in the onActivityResult(..) method in your Activity when it returns to the foreground..

Remove all activities except the first one

I want to program Home button, so it will remove all Activities in stack, except one. I did it like in here: How to finish every activity on the stack except the first in Android
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
Intent intent = new Intent(this, AMainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
...
but this way doesn't suit me, because it removes ALL Activities (including the first one) and starts the first one again. For example - if I check user password in onCreate(), he would be asked again.
How to remove all Activities from stack, but so the first one will not be "touched" ?
add following property to your AMainActivity's Activity tag in your manifest.xml.
android:launchMode="singleTop"
Yes, according to the documentation of Intent.FLAG_ACTIVITY_CLEAR_TOP:
The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().
So, use additional Intent flag:
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
And your activity should not be recreated. Instead, it will just get the new intent delivered via a call to onNewIntent().
You can use ExcludeFromRecent = "true" flag in Activities declaration in manifest, which you don’t want to be in Stack.

Categories

Resources