activity started by other apps, use existing instance - android

My app has two activities MainActivity and ExternEntry. ExternEntry gets called by some other app as a way to launch my app. ExternEntry will then call startActivity on MainActivity, which may have already been launched before. My question is, how to resume the MainActivity instance in the background?
Note that this is different from switching activities in the same application. Every time ExternEntry gets called, does it create a new stack as well?
I tried
Intent i = new Intent(this, MainActivity.class);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
and
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
in ExternEntry, but both create a new instance of MainActivity.
I also tried setting MainActivity to 'singleTask', but that didn't work either.

I think you should declare launch mode in the android manifest files.
<activity
android:name=".name"
android:launchMode="singleTask"
>
i hope it will help you.

Related

Android intents are launched in reverse order

I am working on an android app where I from one activity create and start a number of activities using intents (and close these using the finish() method()) however when I run the app the order the activities are launched in is the reversed of the creation order. I realize this is because the intents are not launched imminently on the startActivity(Intent) call, but added to a queue, but I would really like to preserve the order. My question is, is there any way to keep the order of the activities as they are created? See pseudo code for better explanation
**Main Class**
Intent intent1 = new Intent(this, activity1);
startActivity(intent1);
Intent intent2 = new Intent(this, activity2);
startActivity(intent2);
Intent intent3 = new Intent(this, activity3);
startActivity(intent3);
**Intent class**
....
finish()
However when I launch the app the activity 3 is launched first, followed by 2 and finally 1 ---> how do I preserve the order so they launch 1->2->3?
Thanks a lot in advance
You can start first activity, in it's onCreate(...) method start second and then in onCreate(...) of second activity start third. So you will get 1->2->3 and 3 will be on the top of the stack

Activity trigger more than once if using launchMode="singleInstance

I have a activity which i declare as launchMode="singleInstance in Manifest file. and using blow code to starting this activity:
Intent intent = new Intent(this, LockScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
To close this activity i am doing like: finishAffinity();
But some how when i finish this activity i can see another open activity instance of this. Can someone tell me how i can finish all OR only create a single one when i need to start this activity from different places inside my application?

Does the Intents get killed after i pass to another Intent Android

I'm working on an android application and in the application I have a couple buttons that let user to pass to another activity. Now the way I'm doing the transitions between this Intents is like below:
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
With the above content I start an activity and from that activity I'm getting back to the MainActivity using this code:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
But i suspect this cause memory to be over used because I'm not actually getting back to the Main Activity that created when application started, I'm just creating another instance of MainActivity I guess. Is this really as i thought and if it is how can I get back to the activity that created in the beginning or if I can't do such thing how can I make app to let the previous activity go?
Passing an intent to startActivity() will create a new instance of the activity and add it to the front of the stack. So:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
is basically asking to create a new instance. If you want to go back to the activity just before the current one, call either:
finish();
Or,
super.onBackPressed();
In your solution you just have to press back button and you'll be back in first activity.
If you want to close it and after open new instance like you are doing in second activity just add
finish();
at the end of
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
You just need to call finish(); method
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
finish();

How to bring an Activity back to the front after i click home button(via notification)

I have a VideoActivity which plays a video, what im trying to implement is when i click home button, i will display a notification, and once i click the notification it will bring the VideoActivity instance back to the front.
here's how i define the Intent for my notification:
Intent notificationIntent = new Intent(context, VideoActivity.class);
notificationIntent.addFlags( Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
but everytime i click the notification, it turns out that a new VideoActivity will be created.
Use this as this will also do the same thing in more elegant manner ( without any side effects )
final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
or
you can use this too
<activity
android:name=".YourActivity"
android:launchMode="singleTask"/>
Description of singleTask
The system creates a new task and instantiates the activity at the root of the
new task. However, if an instance of the activity already exists in a separate
task, the system routes the intent to the existing instance through a call to
its onNewIntent() method, rather than creating a new instance. Only one
instance of the activity can exist at a time.
You should try this.
Intent notificationIntent = new Intent(context, VideoActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
You have to make your activity single task means when it is already running or in background state then its new instance should not be created and already running activity should be brought to front.
For this in your AndroidManifest.xml
where you declared your activity add
android:launchMode="singleTask"
in your activity entry.

Android kill all other activities on starting a new one

I have an android activity problem.
Here is how my process works:
Login Activity starts
Login successful. MainMenuActivity starts and LoginActivity is finished by me.
User touched on settings and SettingsActivity starts. MainMenuActivity is NOT finished. because is it the main menu. when user presses the back on settings screen I need to go back MainMenuActivity. so I cant kill MainMenu.
User touched on log out and SettingsActivity is finished by me and Login activity starts. As user returns the login I need to kill MainMenuActivity but I cant.:/
I tried FLAG_ACTIVITY_SINGLE_TOP, CLEAR_TOP, SINGLE_TASK, NEW_TASK, NO_HISTORY etc.. almost all of them didnt work
I put launchMode="singleTask", clearTaskOnLaunh="true" etc. didtn work again.
I tried addFlags() and setFlags() both, didnt work
There are some many issues about this topic here, I read and applied all the suggested solutions and didnt work.
Can anyone help, please?
P.S android:minSdkVersion="8" and android:targetSdkVersion="15" for my app. I didnt use fragments in the app, I use old activity structure.
Use the combination of two flags like this:
Intent intent = new Intent(this, Login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
This deletes all the other activities and starts this one.
Try this.
For api level <11
i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
I'd suggest to start the settings activity for result (see here), and when the user requests to logout, set a result accordingly. You will get this result in MainActivity's onActivityResult (see here) and can handle the logout there, finishing the mainActivity before starting the loginActivity.
I suggest that you don't finish the Login activity when you start the main menu. Then you can always clear all activities on logout by doing this:
Intent intent = new Intent(this, Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will only work if Login activity is still active at the root (beginning) of the task stack.
To prevent the user from BACKing into the Login activity from the Main activity, you can override onBackPressed() in Main activity and do something else.
Use FLAG_ACTIVITY_CLEAR_TOP like this-
Intent loginIntent = new Intent(this, Login.class);
loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
finish();
Intent intent = new Intent(MainActivity.this, MyActivity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
in this example Mainactivity automatically closed and Myactivity2 Start.
and one thing you need to add finish();

Categories

Resources