Appropiate intent flags Android for background tasks? - android

I am working in an Android project. I have 3 Activities (Home, Loading, Menu). The first activty (Home) calls in a background task (when the server answers) to "Menu Activity", but, meanwhile the "Loading" Activity is called for displaying feedback to user. My problem is that when I am in the Menu Activity and I press the back botton, the app shows the Loading activity, and it is wrong. I have been testing different flags, but I have not get the solution.
Can anyone help me, please ?

Related

App appears in task switcher more than once

I am developing my first app now and I see that whenever I click on a new activity it opens up and everything is fine. I can also go back to the main activity and then open a different and it all works.
However once I open the task switcher, all the previous activities show up. They all show up as seperate apps and if I click on one of them that is not the main activity I cannot go back the to the main activity its like its very own up with just that activity.
This is very strange. Does this have to do with how I implement the onDestory method? Because to be quite frank I never used it.
Please help me!
Thank you!

Android stack is not getting clear

I have got stuck in situation please help me .Here is the problem .I have a menu screen in my application.Now have given option to go back to menu to all screens that has not come directly from menu screen .For example I go to Activity B like Menu ->Activity A -->Activity B. Now I have written on pressing backtoMenu button.
this.finish();
startActivity(new Intent(SelectStateActivity.this,MenuScreen.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
I am not finishing Activity A because I want to be on activity A when I press back button on activity B.So it will be on stack when I press menu button on Activity B.
I come to menu screen which is fine but when I press back on menu screen I come to Activity A.Which is not wanted.I also tried
this.finish();
startActivity(new Intent(SelectStateActivity.this,MenuScreen.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP));
and
startActivity(new Intent(SelectStateActivity.this,MenuScreen.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK));
But haven't worked.Please help me.
You can solve the issue by following solution.
Using normal observer pattern which is implemented using core java.
for observer pattern refer this
Or same pattern implemented in android which is Broadcast receiver.
In this solution every activity is registered for your custom broadcast Massage and once you launch Menu activity broadcast that message so that registered activities which can listen message and finish itself.
Let me know if you have issue in above.

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();
}

android - return to app from service or notification

i'm having some problems with the flow of an app i'm working on.
basically , i have a service that always holds a notification , pressing on the notification should return to the app's most recent activity , without re-opening it (meaning that it will resume).
also , on a specific activity (and maybe others ) , i need that clicking on the back button would exit the app (and the service) , so the next time the end user starts it via the launcher or via long pressing the home button , it will go back to the first activity .
in short , the requirements are:
service notification click -> resume current activity , no creation of new activity.
back click on a specific activity -> close app entirely (clearing all app's stacks).
so , for example , if i have activity A which calls activity B (which is the special activity) :
if the end user has clicked home , and then returned to the app via the notification (or launched via launcher/long press on home button) , it will return to the exact state of activity B that he left it .
also , if the end user has pressed the back button on activity B , the app is closed (and the service and notifications shall be gone) the next time he opens the app (no matter how) , he will go back to activity A .
i've tried to use "singleInstance" on activity B , but then it will always get back to activity B , since it is inside its own task , no matter which flags i use(i have tried FLAG_ACTIVITY_REORDER_TO_FRONT and some other flags) .
without using it , the notification will open a new instance of activity B .
can anyone please help ?
an alternative way would be to set the notification's intent to start a new , fake activity , that will close as soon as it is created.
the intent will also have the "FLAG_ACTIVITY_NEW_TASK" flag .
hopefully this method will work for everyone .
too bad this solution seem more like a workaround than a real solution.
another alternative would be this link:
Change notification intent in Android
jelly bean (android 4.1) now introduces a new API for this exact problem :
http://www.youtube.com/watch?feature=player_embedded&v=Yc8YrVc47TI#t=830s
however , i'm not sure i understand how to use it and how it works. is it possible that it re-creates the entire stack of the activities ? isn't it quite problematic since they might include data that wasn't there before (since they are refreshed) ?
it also sounds problematic since it means that i need to monitor all of the actions in order to restore them back later.
ok , even though it's not exactly the answer , for my case , i've used "singleTop" for activity B , and chose to close activity A when moving to activity B .

Android: Recovery Activity When I press Home

i have the following question.
I have an activity which is showing a progress bar while a service is downloading data from an API.
I want that when i press Home and relaunch my program the activity and which is not the first activity called but it is in the stack was recovered in order to continue showing the progress.
I have read about the cycle of life of activities but i don't find a clear solution.
The same thing happens If i am in the activity which is showing the progress bar and i press a button that takes me to another activity, when i go back, can i recover the old activity instead launch one new?
Anyone helps me?
Thanks
The problem is that pressing the home button will erase the whole activity stack. That means there is no possibility to go back to the activity it even is not certain that the activity still exists.
If this a progress that is interesting for the user that it is still running you could display a notification bar icon until the progress is finished. I think you can specify a special intent for clicking on the notification bar and filter this intent with your activity. That way you would go back to the activity. But you still face the problem that the activity is saved and has no reference to the background thread doing the work.
If your Activity has left the stack its finish method is called. You shouldn't try to reuse this activity later on. The best way is to think of a way that the whole state of the activity can be saved and restored later on. To restore a reference to the background thread doing the work you could subclass the application class and save a reference to the running task in your subclass.

Categories

Resources