Android - Resuming already running activity - android

I have an android application which has only one activity having only one button. When we press this button, another thread starts which does some particular work. If the button is again pressed, then the running thread stops.
Now the problem is, if i leave the thread running and go to the home screen and again press the launcher icon of the app, it starts a new activity. Now if i click on the button of the activity, another thread starts. Now I have two threads running at the same time, which i don't want.
I want to reopen the previous running activity and then when I click on the button again, the already running should stop.
I tried using launchmode: singleTask, singleInstance but nothing seems to be working.
Please help how to handle this problem.

Try reading the docs # http://developer.android.com/guide/topics/manifest/activity-element.html
--
"singleTop"
Conditionally If an instance of the activity already exists at the top
of the target task, the system routes the intent to that instance
through a call to its onNewIntent() method, rather than creating a new
instance of the activity.
Alternatively
you can control the thread based on the onPause()/onCreate()/onResme(),
pause in the onPause()
create/start in the onCreate()
restart the thread in the onResume()

Related

Why activity come back to front via onCreate rather than onRestart?

What I want to do is to start an Activity upon device restarts by sending an intent in a BroadCastReceiver listening BOOT_COMPLETED event. The Activity has a conditional moveTaskToBack in its onCreate event handler. The Activity is launchable and the only Activity of App.
When I reboot the device, App is running and I can tell Activity is hidden from screen. From logcat a 'onCreate' message is print to indicate onCreate event happens. Then I click App icon in screen, expect Activity shows up from back for onCreate shall be omitted and moveTaskToBack shall NOT run. But onCreate event handler is still executed and Activity hides again.
In another way I remove starting Activity from BroadCastReceiver, just open App by click icon in screen. At first tap App starts running and Activity hides, and when I tap icon again, the Activity shows up. From logcat the first event is onRestart, which is expected.
I am not sure what is the different between two ways of bring back Activity? Why onCreate happens twice applying BroadcastReceiver in first case?
Cheers!
I'm now aware that the activity started by broadcast receiver is in a stack, while the activity, which is as same class, started by launcher is in another stack. Thus why onCreate event happened again when I tapped App icon desktop, for App create a new stack and brand-new activity.
Thanks xvlcw for great help...

First activity is called after coming in foreground from background

In my application, I am finishing my first activity when I move to the next activity like this:
if(className.equals("com.tritonhk.android.LoginActivity"))
startActivityForResult(in, 1);
//loadingrelative.setVisibility(View.GONE);
displayVal = 0;
Helper.IsFullSync = false;
LoginActivity.this.finish();
So that when I go in background from any other activity and come back in foreground then that activity must be called by which we went in background.
It is happening in some cases but If I remain in background for more then 10 minutes then my first activity's oncreate method is called.
It seems that dalvik is killing my application process that is why when I click on my application icon then its new instance is created and hence its onCreate is called.
Please suggest me what should be the better approach for this.
EDIT Problem solved partially. Now with android:launchMode = "standard" behaves normally but not in first attempt. I mean when I install the app and run it and went to background and come back to foreground then it does not work but from the second time it works properly.
Since you are finishing your current activity which launches subactivity means you don't want the task to retain root activity on re launch ,
you want to start from where you left ,you may use android:alwaysRetainTaskState which will retain state of task since it is useful for root activity only so you have to start your subactivity in a new task using FLAG_ACTIVITY_NEW_TASK intentfilter flag.
Do u have android:launchMode="standard" for the activity which is called while launching the app? If not add this line.

Activity not closing properly

I am calling one activity on click of status bar notification which is having a Complete button. on click of btn. i have folllowing code -
public void completeTask(){
taskDBAdapter.deleteReminder(rowId);
taskDBAdapter.close();
Intent intent = new Intent(this, TaskManagerActivity.class);
startActivity(intent);
finish();
}
whhen i click complete btn new activity (TaskManagerActivity) gets opened properly.But if i reopen my application it still tries to open this activity and not my default landing activity. Any help on this.??
EDIT -
I have tried repositioning my finish() statement . Still its not working.
EDIT 1.1 -
Ok I will provide some details here. Assume my app has two activities
Main Activity
Notification Activity
My app create some notification to display on Status bar. So as soon as i click on status bar Notification actvty will open. Now there is a button called Complete on click of which the code given will fire and main activity (in the code TaskManagerActivity.class) will open. But after I press back button in my app and again reopen it , it opens the notification activity when it should have fired the main activity (as it is launching activity).
Thanks,
Ray
That's the default way android functions. If you press the home button and then open your app again, it will restore the apps previous state (unless it has killed the apps processes and activities due to memory constraints). So you are not actually restarting your app but only restoring it.
If you wanna quit the app, then press the back button. Now when you re-open the app, the original activity will be launched.
Do not modify this behavior. It is the default system behavior and users expect it to work this way. Your app is fine :-)
- First of all the behavior which you are experiencing is the way Android is made to function, moreover when a user gets a call while this app is open, after finishing the app he will definitely want to get back to the state where he left the application.
Still if you want it that way, here it is.....
- Make sure your application has only single instance of it running, by using android:launchMode="singleTask", or android:launchMode="singleInstance"
- Then finish() your Activity at onPause().
#Override
void onPause()
{
super.onPause();
finish();
}

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.

Suddenly stopped hitting onCreate

I have a home screen widget with a button with a pendingIntent. Click it, it bundles a boolean to indicate where it came from and it brings up my main activity.
In my main activity in onCreate, I check for a bundle, if the boolean that says it was launched from my widget is true, I perform an action.
Every time I hit my widget button, it should launch my main activity, and run through onCreate.
And it does, on the emulator. And it did, on my n1. Then suddenly it stopped on my n1. I can still get it to hit onCreate when I launch from the widget but I have force stop or reinstall and then it only does it the first time.
What is going on with this?
What's going on is the Activity lifecycle. Android will keep the same instance of your Activity around so that the user can return to it later. Monitor some of the other lifecycle methods like onStart and onResume to see this in action.
You may see a difference in behavior if the user hits the back button vs. the home button. The back button will finish the current Activity by default, whereas home will leave it alone unless Android decides to kill it for resources later.

Categories

Resources