launching already launched activity from notification creating problematic activity - android

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

Related

how to know if Activity resumed by shortcut or by another top activity

The main activity creates a shortcut to homescreen, all goes good but it needs to know an extra value when it starts by shortcut. So I used this while creating shortcut
intent.putExtra(EXTRA_ID, "Some string info here");
And onResume retrieve value like this
if (getIntent().hasExtra(EXTRA_ID))
ShowToast(getIntent().getExtras().getString(EXTRA_ID));
Everything works fine, lets take some examples
Activity never launched, Home screen shortcut pressed and A toast appears, Nice.
Activity is in background, Home screen shortcut pressed and A toast appears, Nice.
Activity is in use, another activity is launched, when back, toast appears, Not nice.
Activity is in background with no extra, Shortcut pressed, Nothing appears.
In 3rd example, it does what it suppose to do. But how do I know if activity is resumed/launched by shortcut ?
You set an extra in launcher Intent, and want to get the extra when this activity is opened in any state.
The problem you meet:
Activity is in background with no extra, Shortcut pressed, Nothing
appears.
This is because that your activity is started already, the launcher intent just use the existing task as the front task, and its extra data is lost in such situation.
To solve this, you need 2 steps:
Add android:launchMode="singleTask" to this activity in your manifest.
Put the extra data handling codes in both onCreate() and onNewIntent(), not in onResume().
How are you removing the extra EXTRA_ID? You should do it like:
In onResume():
if (getIntent().hasExtra(EXTRA_ID)) {
ShowToast(getIntent().getExtras().getString(EXTRA_ID));
getIntent().removeExtra(EXTRA_ID);
}

Acitivity Lifecycle (moving back and forth between Activities)

As Android newbie I started to wonder about the Activity lifecycle. I'm having an Activity that loads a list of Persons and displays them. Upon the click of a Person I want to open another Activity showing the details of that Person. I'm currently doing this by creating an Intent on the "PersonDetailActivity" which I then start. So far so good. On the PersonDetail page I would like to have a menu action to go back to the Person list. I again applied the same technique, meaning an Intent that brings me back to the PersonListActivity.
Now I started to wonder what returning to the PersonListActivity means. Will a new instance get created and will I have to reload the persons that it displays in the list? Can you come back to the same instance, avoiding having to reload the list again? Do you then have to pass a pointer to yourself via the intent to the other Activity?
So when will and Activity be re-instantiated and when will it not. Any hints or suggestions are more than welcome. Maybe there are some patterns to be applied for these back and forth menu actions that I'm not yet aware of.
Thanks,
Vincent
Yes,,. Call finish() in second Activity instead of starting new Activity..
There is basically something called Activity stack which stores all Activities in the order they were started.. so if start new Actvity , that sits on top of the stack and preveous one gets below it.. when you call finish the Activity is poped out..
if you don't want to call finish() correct waht ever you were doing then add flag ACTIVITY_CLEAR_TOP in manifest for the 1st Activity..
Basically if you just call the finish() method on your PersonDetailActivity
PersonDetailActivity.this.finish();
it will activate the onResume() method from the Activity that is on the top of the finished one, which here would be your PersonsActivity. You can specify in your onResume() method what you want to perform when turning back there.

Close the whole stack of activities in Android without graphical issues

I am trying to close a whole stack of activities using the way described here:
How to kill an application with all its activities?
Namely, each activity starts the other one with startActivityForResult, and in onActivityResult calls finish() to close itself together with the activity it opened.
The problem is that the activities in the task still seem to repaint themselves at least once before they close, and this doesn't look good. After closing the topmost activity one sees all previously opened activities like in a very fast slideshow.
How can one avoid this graphical issue?
EDIT: I need that if the user presses HOME button and then presses the app's icon in launcher, he returns to the current state of the stack, not to the very first activity again. So, from what I understand, with such a requirement I can't finish() activities before starting next ones.
That's native behaviour, intended to aid in user Experience. When an Activity is started with startActivityForResult and then finishes, it will (on devices that allow fancy animations) automatically slide away. That helps people not get surprised by the screen suddenly changing.
You could try starting the Activities without startActivityForResult and handling the passing of data to and from Activities manually, then handle how/when Activities finish() and which Activity they pass back to. You might find you implement something where Activities actually pass forward to the appropriate Activity all the time, rather than back to an Activity on the stack.
Intent intent = new Intent();
intent.setClass(getApplicationContext(),
PhoneListCheckboxAES.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
If u give like this when u are starting the next activity then the graphical problems won't occur

Android: How do I totally remove an Activity from the Activity Stack?

I have two Activities FirstActivity and SecondActivity.
FirstActivity has an intent filter MAIN, LAUNCHER and DEFAULT.
SecondActivity is just a normal activity and no other flags is set in the AndroidManifest.
My application is C2DM enabled and when a notification is received a taskbar Icon is displayed and when clicked it opens SecondActivity.
Arrival of Notifications has two scenarios:
First, My Application is Already Running when notification arrived and second My Application is totally not running.
On the first scenario, everything is fine. A notification is received, i clicked from the notification drop down and my SecondActivity is displayed. When I press back SecondActivity is finished and application goes to the background. When I start my application again from Recent Applications, it displays FirstActivity which is correct since its the MAIN, LAUNCHER and DEFAULT.
On the Second Scenario, A notification is received, I clicked it from the notification drop down and my SecondActivity is displayed. When I press back SecondActivity is finished and application goes to the background. When I start my application again from recent Applications, it displays SecondActivity which is wrong. I am expecting FirstActivity to be displayed because it should be a fresh start.
I tried ForceKilling the application after the second scenario but the result is the same, I needed to restart my phone to be able to start from FirstActivity Again.
Have you guys encountered the same problem? or is it just me? What do you think is wrong with my configuration?
I also tried setting noHistory=false to SecondActivity but still the results are the same.
When you start application from 'Recent Application' shortcut, it will launch the last activity. So you can try to launch the application from the application list, see whether it solves your problem.
It is the correct behavior.. if you start the application through "Recent Application" it will point from the last activity.
Try launching it through normal application list
For your main Activity try to set the lauch mode as "singleInstance" in the AndroidManifest file. for second activity set "Finish On Task launch" to true.
In Second scenario i think activity stack is empty as app is closed . so now you are starting activity2 , means this is the only activity in stack . now desired back behavior can be possible by overriding onbackpress and start activity1 if its second scenario .
you can call your second activity with the flag set no history like this,
intent1.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
This has worked for me several times.
Read from the Android Doc

Extras based REORDER_TO_FRONT

In an app I have very reused Activity, that shows a list of stuff happening on a specific day. The day is specified using Intent Extras.
My problem is, that if the user starts at day=1, then chooses day=2 and then day=1, from the menu, then I would like the back button to go to day=2 and then home. That is, I want to do REORDER_TO_FRONT, but not just based on the name of the activity, but also its extras.
There doesn't seam to be any intent flags suitable for this purpose. I've considered implementing my own 'sub activity stack' using onNewIntent, but it probably wouldn't work very well.
Have you tackled similar problems in your apps? Is there perhaps a way to programmatically access the activity stack, and choose which one is suitable to return to?
Manage your own Activity stack! If I'm not mistaken, you use the same Activity to display each day. Make it single top (FLAG_ACTIVITY_SINGLE_TOP). In the launching intent, pass on the current stack, in your example "121".
Respond to the back button event by launching your Activity with a smaller stack: "12" - or if stack is already "", then just let the Activity handle Back event. Then as you mentioned, use the onNewIntent function to update your Activity.

Categories

Resources