I have an app that contains several activities...so lets say user is navigating activity stack A->B->C. Then presses the HOME button. Now when the user clicks on the Widget, I just want bring my app's existing/current instance to the foreground. I do NOT want a new instance of the app. I want activity C to get back to foreground.
I tried launchMode to "singleTask" | "singleInstance" but that does NOT solve my desired requirement since it clears the current instance and creates a new one with activity A.
Any ideas how to solve this?
Thanks much!
Using a "singleInstance" is not such a good idea (for many reasons).
Here is a better way - from your widget intent handler:
Intent yourActivity = new Intent(context, YourActivity.class);
yourActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(yourActivity);
You don't need to add CLEAR_TOP to recall activity C, but you need it if you want to bring A or B.
I am only posting because between some 10+ similar posts nobody actually pointed the correct way of doing it.
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP - last paragraph.
I managed to figure out a solution. I set the specialized launch mode....
android:launchMode="singleInstance"
Every time the user clicks on the widget or the app icon again, the default activity A is started. To open the app up where it last was, inside the onCreate() method in your MainActivity class just check isTaskRoot(). If false, call finish() and the activity A (MainActivity) will not appear and the app will open up to where it was in activity B.
Try adding this to your intent
intent.setFlags( Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Related
i have 3 activities A-B-C .Lets say i am on activity B and from a listview there i launch activity C now if a notification comes which has an intent of launching activity C.
So the problem is I am getting multiple instances of activity C
When i use launchMode singleTop or instance or task the problem of multiple activity instance is solved but the newly launced activity does not work properely as it is desired to be.
Please help me out tired of using flags and stuff but not able to overcome this problem.
The scenario is Just like whatsapp , if u r talking to person one and a message of person 2 come as notification ,when u click on that notification the activity relaunches and works properely. This is exactly what i want. Please help me out how to achieve this . :(
Thanxx in advance
What Flags did you try and what exactly is not working, means, how does the Activity behave?
What you describe with WhatsApp could be achieved with two steps:
Use the FLAG_ACTIVITY_SINGLE_TOP for the Activity.
Overwrite the Actvity.onNewIntent(Intent intent) method in the Activity. This method is called instead of creating a new Activity and gives you the new Intent. I assume that the Intent contains all necessary information in the extras.
The user experience should be as follows:
User chooses from the list, which opens the new Activity
He/she can press home or back button from here. This brings the home screen or the list back.
If, for any reason, somebody else calls startActivity for your Activity while it is running, the onNewIntent is called. There you can add code to refresh the content, maybe have an Alert that tells the user that the content has changed, and shows all that to the user.
If the user presses back or home now, he/she will get to the list or home screen
If that is, what you're looking for, then the flag and the method is what you need
I have an application that implements somewhat like having a top page.
So like, i have this activities:
TopActivity , FirstActivity, SecondActivity, ThirdActivity and FourthActivity.
Each activities has a button that when you press it, it will bring you back ti the TopActivity.
The way I implemented this one is every time that button is pressed, I start an activity, a new TopActivity. So, every time a top activity button is pressed, it always create a new activity. If I also will finish() the TopActivity when it goes to first, second and third, I can not go to TopPAge using back button. Are there itger ways to do this?
Any help is greatly apprieciated.
You can use a different Launch Mode for you activities. The launch modes decides when and how to create a new Activity or reuse a previous one.
The singleTask launch mode seems like it would do the trick for you:
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to existing instance through a
call to its onNewIntent() method, rather than creating a new one.
I am new in android and I have total 6-7 activities in my application. I want to know how can I manage my activities properly means when I move to the A->B->C->D like that. Then how can I move that the stack of these activities not created.
On moving from one activity to the other I am using the below code:
Intent intent=new Intent(current.this,next.class);
startActivityForResult(intent, 0);
And now if I want to move back on the earlier activity I used the code as:
Intent start = new Intent(current.this,next.class);
startActivity(start);
finishActivity(0);
Is there a special reason that you don't want to use the activity stack and let the activities handle themselves?
The Android system has done a very good job with the activity lifecycle. It allows you to start an Activity from different places without confusing the user because the back button will bring the user back to a different activity.
If you don't have a very good reason to not use the Android guideline try to stick to the way the system is doing it. Every other thing will only give you problems.
You are starting activities for a result but how I understand you you will never return to them.
You can start an Activity and after that just finish the current Activity. That way the activity will not be put on the back stack. Now you need to listen for back button pushes and create the activities that you want to bring the user to.
If you want to move from Activity A to D like going to the start/home screen of you app you do the following:
Intent goBackToA = new Intent(context, StdActivity.class);
goBackToA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(goBackToA);
The flag FLAG_ACTIVITY_CLEAR_TOP will tell the system that if the backstack contains an instance of the Activity this activity will be shown and all activity that are between the current activity and the target activity are removed from the backstack. This allows you to go back to a home activity without creating huge loops that the user can move through with the back button.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapear.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapeear or you can press Back button to see the previous Activity .
whenever you want to navigate from one class to another use this code, may be this help you to navigate the Activity,
Intent nextpage = new Intent(CurrentActivity.this,NextActivity.class);
startActivity(nextpage);
this.finish();
I'm working on a widget for an Android app and I encounter a problem. When the user click on the widget, I'd like the application to open his first activity (the one with intent-filter: 'action.MAIN' / 'category.LAUNCHER') (I'll call it Activity A).
To do that, I use this snippet (in my WidgetProvider class):
Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.view , pendingIntent);
This works very good if my application is killed or if all the activities have been closed (using the back button until the application close)
The problem happen when several activities are open. Imagine the activity stack is like : Activity A -> Activity B -> Activity C.
If I click on the widget, SOMETIMES the application is just brought to front and Activity C is visible instead of Activity A.
It's very strange because this doesn't happen every time. (It seems that it happen after I navigate a lot in activities)
I really need the activity A to open and not another activity whatever how the activity stack was because this activity is like a hub with several links to differents fonctionnalities.
Can somebody help me?
Thank you very much.
PS: Sorry if my English is pretty poor, I'm from Belgium
From a widget you should do more or less the same as the system launcher does with intents.
Try using FLAG_ACTIVITY_CLEAR_TOP in your intent. You will also want to look at FLAG_ACTIVITY_NEW_TASK.
It's better to follow the platform guidelines by the way, which say that you can also inject deep into your app, provided you give the user the right path to navigate up again.
If you want a specific Activity to launch, why don't you just call that activity specifically, instead of dynamically.
You are experiencing issues due to the Activities in your back-stack. Android manages the life-cycle of your Activities in a very special way that you should probably research - http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle
calling finish() - will always return to the activity that launched the current activity. The only time your app will close, is if there was not an original activity. You could always test for this condition, and if the calling activity is NULL, you could explicitly call you Activity A.
Scenario:
Activity "A" launches Service "S".
Press 'Home'/'back' key to not to display "A".
Some time later "S" start intent Activity "B"
Intent intent = new Intent(context, MyDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Current behaviour: "B" shows up with "A" behind in the scene.
What I want: "B" shows up without "A" benhind in the scene.
Note: Activity B is a Dialog Activity
First, you shouldn't do this. Services shouldn't start activities (in general). You don't want windows popping up on the user unless it's specifically asked for (ie texting app).
You could use the noHistory attribute on activity A if that suits your requirement. http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
But as you're describing it, what you're seeing shouldn't happen, ESPECIALLY if you navigate away from A with the back button.
I believe you should be able to call finish(); as the last line below what you have here. Don't quote me on that though.
You can get answer from Android Service and AlertDialog Activity. Quoting the comment of Albin:
Add launchMode="singleInstance" in the manifest for the DialogActivity