On resart activity how to clear stored variables? - android

When I restart an Activity using
Intent intent = new android.content.Intent();
intent.setClass(this, this.getClass());
this.startActivity(intent);
A lot of the state is remembered, but I want to clear it all (its a rare exception handler that I am using to try to clear all).

What you'd have to do is kill the activity in the onPause() method (by calling finish()) . What happens is that when you call your intent and your activity was already created but it's sleeping, it doesn't call its onCreate(), it just calls onResume() and all the state would be there.
This may have other consequences, i.e. if the user is working on your app an a text or a phone call arrives your activity would be killed, losing all the state.

Related

android Prevent a background app from launching activities in the foreground

I have an activity in my application that does a process and when finished, launches an activity with a normal intent:
Intent intent2 = new Intent();
intent2.setClass(anActivity.this, JustAnotherActivity.class);
startActivity(intent2);
finish();
The problem is that when you are doing the process, if I press the home button and take it to the background, when it finishes and calls the new activity, even if it is in the home of the phone, this new activity jumps to the foreground.
I am looking for the activity to be launched, but if the user is outside the application, it does not come to the foreground.
I guess the use of a FLAG is necessary, but the ones I have tried have not worked.
sorry for my bad english
Thanks.
what if you take care of ActivityLifecycle (OnPause, OnStop).
You can add a flag when the activity goes "OnPause/OnStop" depending what you want. And them perform the intent just if the activity is not "Pause/Stop".
Activity Lifecycle

NFC intent calling onPause before onNewIntent - How to know the origin of the onPause call?

While scanning an NFC tag in an application with Foreground Dispatch enabled, onPause() is called before onNewIntent(). Is it possible to get the NFC intent already in onPause() so that I can stop (or not) processes according to the origin of the intent?
Since the activity is in the foreground and should not be paused/resumed when a tag is scanned, and since I have tasks that have to stop or start in onPause/onResume, you can see the problem.
The getIntent().getAction() is always "MAIN", but surely there must be a way to get a more accurate description of the intent? Or is not possible to know it's an NFC intent before entering onNewIntent()?
No, you won't be able to know what the new intent is before it is delivered to your activity in onNewIntent(). That's the point of the onNewIntent() callback: Deliver the information about the new intent to your activity. Only when this method is called, you can obtain the new intent through the intent parameter of that method:
#Override
protected void onNewIntent(Intent intent) {
...
}
Note that getIntent() will always return the intent that initially started your activity unless you explicitly update that intent using setIntent(newintent). So if you started the activity through the launcher you will get the intent action MAIN.
onPause/onResume() is called due to the way the NFC service dispatches the intent to your activity. There is nothing you can do about that. In fact that should not be something that you need to differentiate in your application logic. If onPause is called you should do whatever you would normally do in there regardless of whether this was invoked due to explicit user UI interaction moving some other UI component in front of yours or due to the system moving some (invisible) UI component in front of yours. If your current onPause logic really needs to differentiate between the two, then it might probably better be done in onStop.

How to put in foreground an Android activity that is in background

I read several similar questions here, but I didn't find a clear reply to my question.
I launch my Android App and I have my main ActivityA in foreground
after some time I push a button (of ActivityA) and I open (and put in foreground, then visible and ontop) ActivityB. I do it simply by the command myContext.startActivity(myIntent);
It means that now ActivityA is in background (onPause()), then not visible.
After some time I push another button of ActivityB with the target to put in foreground (then visible and ontop) again previous ActivityA
What is the correct and best way to do it? According to my understanding (but I'm not sure it's correct.) it shouldn't be by startActivity(), because startActivity() creates another instance of ActivityA (it calls onCreate() ) and then there will be 2 instances of ActivityA running (one in foreground and one in background). What I want to get is a calling of onResume() for ActivityA (and not of onCreate() ).
The second question is: how can I know if ActivityA is still alive in background? Maybe after sometime the system killed it to free resources.
Note: the solution in my case cannot be to use finish() to destroy ActivityA when I open ActivityB, and then to use startActivity() to reopen it, because I need ActivityA alive as much as possible.
Thank you very much in Advance
Fausto
What you need is the FLAG_ACTIVITY_REORDER_TO_FRONT when starting a new activity. This will cause a background activity to be be brought to the foreground if it's running, or create a new instance if it's not running at all.
From inside ActivityB:
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
What is the correct and best way to do it?
Use startActivity(), with an Intent on which you have added Intent.FLAG_ACTIVITY_REORDER_TO_FRONT.
it shouldn't be by startActivity(), because startActivity() creates another instance of ActivityA
While that is the default behavior, Intent flags can alter that behavior.
how can I know if ActivityA is still alive in background?
If you did not finish() it, and your process has not been terminated, it exists.
Maybe after sometime the system killed it to free resources.
Android terminates processes to free up system RAM. It does not destroy activities on its own.
because I need Activity A alive as much as possible
To be honest, that suggests that you have other architectural issues. Bear in mind that activities are destroyed and recreated for various reasons, such as configuration changes (e.g., screen rotation). Activities should be very disposable.
You can use following
1. For launching new instance (current state of ActivityA) and get ActivityA on Top of Stack
Intent intent = new Intent(this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
2. For launching old instance and get ActivityA on Top of Stack
Intent intent = new Intent(this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
For more detail please check
Task and Back Task
You can use StartActivityForResult in the place of StartActivity in activity A and on activity B you can setResult when you want to open Activity A again.In that case OnActivityResult() of activity A is called not onCreate().

In an activity, if the activity is paused and then invoked with an intent, getIntent() returns the old one

I have an activity that shows some information based on the intent that initiates the activity. The app icon starts this activity without any extra information and the activity displays correctly. The app widget also starts this activity, but with some extra information and is handled correctly. In this scenario, the app does not work as expected:
The user enters the activity using home screen shortcut.
The user uses the home key to send the app to the background.
The user uses the widget shortcut to enter the app
in this scenario, in onResume() the intent is the old one not the one from widget.
How can I solve this problem?
This is exactly why onNewIntent() exists: it is called:
when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.
Therefore add code such as:
#Override
protected void onNewIntent (Intent intent) {
setIntent(intent);
}
And/or move your code from onResume() into a method such as handleIntent() and call it from both your onCreate() and from onNewIntent().

Restarting Android application after process is killed

When my application is idle, Android kills the process.
If user reopens the application after some time, only the top Activity is created - this is a problem for me because the activity depends on initialization of other objects (which are now destroyed).
What I want to do in that case is to re-launch the application.
How can I do that?
Just identify that your Application is being launched after it was previously destroyed by Android, you could do this by keeping a variable in a custom Application class, and set it to true after your applicaiton is initialized. So when the applicaction is re-launched, this flag is false, and then just make an Intent to launch your main Activity specifying FLAG_ACTIVITY_CLEAR_TOP :
Intent reLaunchMain=new Intent(this,MainActivity.class);
reLaunchMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(reLaunchMain);
I think this answer only for you.
After finish progress call this
finish();
Intent intent = new Intent(this, sameactivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
You should probably be looking at storing such Objects in your app's implementation of the Application class.
If these objects contain state that needs to be more persistent, you should save the state of such Objects in each Activity's onPause() method, either to the database, in SharedPreferences or remotely.

Categories

Resources