How to tell if my main activity was killed by the system - android

In my app I have one main activity and second activity. My second activity is transparent and the main activity is shown under.
My problem is that sometimes when the user gets back to the app strait to the second activity, the system has destroyed the main activity and now the main activity is not shown throw the transparent part.
I want to make sure that if My main activity is destroyed by the system, then I should know about it or the app should restart from the main activity.
Is it possible? How?

When your second activity goes into onPause, kill it with finish().
Then use your MainActivity as a 'controller' and when onResume is called , i.e. when a user returns to your app start an Intent for your SecondActivity , that way you can always guarantee they are both there

Related

Start activity of another task and stay within current task

I have an application with, let say, two activities. One of the activity is already launched and after pressing "home" button it is in background. Now, from the ANOTHER application the second activity is launched. And when this second activity is finished I got to the first launched activity, not in the application that started the second activity. Is it normal behaviour? If it is, is there some way to change it? I need to stay in the application that launches the second activity, and not in the application which this second activity from.

Tracking the activity life cycle

I have requirement wherein I have to display a lock screen if the user moves out of the application.
Hence, the structure is: Activity A extends Activity B.
Wherein Activity B is the deciding activity : "was application in backgound".
If so it launches the lock activity.
Now, say I am on activity A and receive a phone call. Hence the app gets into the background.
When it resumes I can see the glimpse of Activity A for a fraction of second and then comes the lock activity.
Can there be any solution to avoid that glimpse of Activity A?
You can see the lifecycle of activity from official doc
You are using activity B just to track whether activity is alive. I am not sure if it is necesary.
If activity goes to background onPause() method is called, it means activity is not visible (it might be both screen lock or home button pressed), and onResume() is called when activity is visible again. In Activity A if you override onPause method and launch your lock activity, it should work. (Or set a boolean onPause and launch lock activity on resume(you might see Activiy A though)
Good luck

Android how to save and restore state of Activity when calling finish()?

In Android, from Activity A, if I start new Activity B, Android will automatically save all the state of Activity A. So when I click back or call finish() from Activity B, Activity A will be restored to the state when I start Activity B (for example: position of a ScrollView in Activity A, or the value of ProgressBar). In Activity A I have a ProgressBar showing progress of some task, then if I come back from Activity B, the ProgressBar is restored and keep running.
Now what I want is, from Activity A, I will call finish(). How can I save all the state of Activity A just like Android did (include the ProgressBar as I describe above), so when I start Activity A again, I can restore everything like before calling finish(), and my ProgressBar keep running?
Can I push Activity A to something like stack in Android and then pop it out? Anyone know how to achieve this?
Do not call finish on clicking the back button.
Instead call the method "moveTaskToBack(false)". This will 'minimize' your application.
Next time you open the application the application opens with the previous state.
The whole point of finish() is to say that your Activity is "done and should be closed". If you aren't done, just don't call finish().
When you go from Activity A to Activity B, Android does not kill Activity A. Activity A still lives in the background. It can however be killed at any moment if Android needs the memory.
To save the state of an activity you have to do it manually.
Here's a similar question with a good answer: Saving Android Activity state using Save Instance State
If you want to finish your activity and then start it again just to load some new configurations, You can call activity's recreate() method instead of calling finish(). Calling recreate() will call onSaveInstanceState() and onRestoreInstanceState(), helping you to restore your previous configurations..
You can look at this answer for more detail : How do I restart an Android Activity

How to force close an app

When my app crashes, it loads again in a random activity instead of being force closed.
When I call android.os.Process.killProcess(android.os.Process.myPid()) it follows that behaviour too.
I just want my app to force close if needed and let the user send me their reports.
What should I do to avoid that behaviour?
Are you sure it loads some random activity? Usually it loads the next activity on top of your app's activity stack.
from https://stackoverflow.com/a/7240460/262462:
start first activity (either splash screen, or whatever activity is currently at the bottom of the activity stack) with FLAG_ACTIVITY_CLEAR_TOP (which will quit all the other activities started after it, which means - all of them). Just make to have this activity in the activity stack (not finish it for some reason in advance).
call finish() on this activity

How to finish() every Activity on the stack associated with the Application?

Is there any way to tell Android, "If the user exits this Activity by hitting the 'home' key, immediately finish() every Activity on the stack that has ever been associated with this Application?" Or, alternatively, a way for an Activity whose onPause() method has been forcibly called by a user hitting 'home' to finish() not only itself, but the Activity that started it as well?
The problem I have is workflow. My application's users basically go back and forth between a main Activity and "detail" Activity windows. They see something in the main Activity, tap it, the child detail Activity gets spawned, they use it for a while, then hit back to return to the main Activity. I can't finish() the main Activity after starting the child Activity, because then the back button would quit working. The problem is that now, if the user exits by hitting the home key in a child Activity, it leaves behind a zombie parent Activity. If the user relaunches the application, uses it for a while, then explicitly triggers a shutdown by hitting menu->quit from the main Activity (where I kill the service, then call finish()), the Activity goes away, and the user gets dumped into the zombie Activity from the previous run (which promptly crashes if the user tries to do anything, because the background service it depends on has already been shut down, and all the BroadcastReceivers that would have otherwise been listening have been removed, so its cries for help go unheard).
I'm actually kind of puzzled, because I already have android:launchMode="singleTask" sitting in the manifest tag for my launch activity, and I thought launchMode="singleTask" was explicitly supposed to prevent things like this from happening by making sure that any lingering Activities from a previous run were safely dead before it relaunched.
The easiest way of doing this is:
Intent i =new Intent(CurrentClass.this,HomeClass.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Perhaps you can mark all the Activities in the stack with clearTaskOnLaunch (see http://developer.android.com/guide/topics/manifest/activity-element.html#clear).You can mark all the Activities that would be in the task with the attribute.

Categories

Resources