I have an activity (A) that is declared in AndroidManifest.xml:
<activity
android:name="A"
android:launchMode="singleTask">
</activity>
I want to from other activity in application start A and override android:launchMode="singleTask" to android:launchMode="singleTop"
How can i do that? Thanks in advance.
What worked for me is to finish the current activity and post start activity to the activity ui thread.
// get rid of this activity instance and start a new instance one
finish();
final Intent intent = new Intent(this, MyActivity.class);
runOnUiThread(new Runnable() {
#Override
public void run() {
startActivity(intent);
}
});
See FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_MULTIPLE_TASK at http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK
If you want to be able to start that Activity in an existing task, then you need to change the launchMode back to standard or singleTop. If you set the launchMode to singleTask then you are telling Android that you do not want your Activity to be launched into an existing task. Being able to override that would be counter-productive. That's the whole point of this launchMode!
Related
I want to make so the every activity launches with only one main activity in the backstack, so I always can return to the main activity with back button as for example (for starting activities I use startActivity()):
Main Activity - Activity1 (back pressed) returns to Main
Activity
Main Activity - Activity1 - Activity2 (back pressed) also
returns to Main Activity
It looks like I need to use FLAG_ACTIVITY_CLEAR_TASK flag on launching every new activity, but it clears Main Activity either. I've tried FLAG_ACTIVITY_CLEAR_TOP works fine in the 1st case, but not in the 2nd.
If someone has the same problem, please help. Any thoughts appreciated! Thanks!
My main activity:
<activity
android:name=".activity.main.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTop">
<meta-data
android:name="android.app.default_searchable"
android:value=".activity.search.SearchActivity" />
</activity>
Set FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY flag while firing the intent of Main Activity in onBackPressed().
Just make sure that you call finish() when Activity1 starts Activity2. This will ensure that your task stack looks like this: MainActivity->Activity2.
As #EmmanuelMtali mentioned, I used parent activity for this reason.
For activity different from Main, I set meta-data (and android:parentActivityName, API > 16) as follows:
<activity
android:name=".activity.user.LoginActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTop"
android:parentActivityName=".activity.main.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.main.MainActivity" />
</activity>
When I need to start new activity I use array of intents and startActivities() method:
public static Intent[] createIntentsWithMainActivityInStack(Context context, Intent topMostIntent) {
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
topMostIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
Intent[] intents =
taskStackBuilder
.addNextIntentWithParentStack(topMostIntent)
.getIntents();
return intents; }
and
Intent[] intents = createIntentsWithMainActivityInStack(context, new Intent(context, LoginActivity.class));
startActivities(intents);
Try this in every activity so that when you back press main activity opens:
#Override
public void onBackPressed()
{
super.onBackPressed();
Intent intent = new Intent(ACTIVITYTHREE.this,MAINACTIVITY.class);
startActivity(intent);
}
This is my AndroidManifest.xml
<activity android:name=".ui.activity.ERPWebContainerActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="#style/Theme.Eallcn.Fullscreen">
</activity>
When I step into a new Demo1Activity, I deal with some data, Step to Demo2activity from Demo1Activity.
when I in the Demo2activity activity. I use this method: startActivity() doesn't work.
Intent intent = new Intent(this, ERPWebContainerActivity.class);
intent.putExtra("waitUploadImageLists", waitUploadImageLists);
startActivity(intent);
finish();
You can use android:launchMode="singleInstance"which work almost same.
but still if your requirement is to use singleTask only then you can use that in this way:
android:launchMode="singleInstance"
Just remove the launchMode="singleTask" and set FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP flag whenever call an intent to your activity
You can override this method, you can get the Intent data!
why ? because the activity didn't finish, When i startActivity form Demo2activit, It does't go to onCreate method!
you can get the reslove way: you can see Android: lunchMode
enter link description here
Try in this order.
finish();
Intent intent = new Intent(/*intent parameters, but as I can't copy and paste it from a picture then... */);
startActivity(intent);
I am launching an activity from a Service using the normal pattern:
Intent i = new Intent(getApplicationContext(), MyActivity.class);
startActivity(i);
I want to animate that launch e.g. slide in from left.
From an Activity, you can use Activity.overridePendingTransition().
Is there a way to animate an activity launch from a Service?
I went with the workaround of placing my own activity into the launch process.
My Service launches my HelperActivity with extras in the intent identifying the task I wish to launch/bring to front.
HelperActivity launches the actual task in onCreate(), animating the change with overridePendingTransition(). Then calls finish() in order to remain hidden.
Obviously HelperActivity needs to remain hidden from the backstack, so the manifest reflects this.
There are also security concerns to this naive method - see related post by Commonsware:
Beware Accidental APIs
This type of activity should be as secure as possible. For my demo version, I'm happy to just set android:exported="false".
Manifest Code:
<activity
android:name=".SwitchAnimationHelperActivity"
android:excludeFromRecents="true"
android:exported="false"
android:noHistory="true" >
</activity>
HelperActivity Code:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String componentName = getIntent().getStringExtra("TASK_ID_API");
Intent newTask = new Intent(Intent.ACTION_MAIN);
newTask.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newTask.setComponent(ComponentName.unflattenFromString(componentName));
startActivity(newTask);
overridePendingTransition(R.anim.anim_zoom_in, R.anim.anim_slide_out_left);
// job is done
finish();
}
So there it is - one way to animate the launch of a task from a Service.
I have a common menu on my app with icons. Clicking an icon will start an Activity. Is there a way to know if an activity is already running and prevent it from starting multiple times (or from multiple entries)? Also can I bring an activity that is in onPause state to the front?
Use this:
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
while starting Activity.
from documentation:
If set in an Intent passed to Context.startActivity(), this flag will
cause the launched activity to be brought to the front of its task's
history stack if it is already running.
In your activity declaration in Manifest file, add the tag android:launchMode="singleInstance"
I got it perfectly working by doing the following.
In the caller activity or service (even from another application)
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(APP_PACKAGE_NAME);
//the previous line can be replaced by the normal Intent that has the activity name Intent launchIntent = new Intent(ActivityA.this, ActivityB.class);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launchIntent);
and in the manifest of the receiver activity (the I want to prevent opening twice)
<activity android:name=".MainActivity"
android:launchMode="singleTask"
>
This works for me :
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
from official documentation
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
also, you can use FLAG_ACTIVITY_NEW_TASK with it.
then the code will be :
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Just use
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
create an instance of your activity which you dont want to start multiple times like
Class ExampleA extends Activity {
public static Activity classAinstance = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
classAinstance = this;
}
}
Now where ever u want to crosscheck i mean prevent it from starting multiple times, check like this
if(ExampleA.classAinstance == null) {
"Then only start your activity"
}
please add this in menifest file
<activity
android:name=".ui.modules.profile.activity.EditProfileActivity"
android:launchMode="singleTask" // <<this is Important line
/>
In an given Android activity, I would like to start a new activity for the user at some point. Once they leave the first activity and arrive at the second, the first activity is stale and I want to remove it completely so it can not be accessed again from the back button.
How is the best way to accomplish this? How do I kill or destroy this activity immediately after the user has launched the new activity?
You just need to call finish()
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();
Setting android:noHistory="true" on the activity in your manifest will remove an activity from the stack whenever it is navigated away from. see here
you can use:
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
You can also add android:noHistory="true" to your Activity tag in AndroidManifest.xml.
<activity
...
android:noHistory="true">
</activity>
Yes, all you need to do is call finish() in any Activity you would like to close.
Write this in each "new activity" after you initialized your new intent->
Intent i = new Intent(this, yourClass.class);
startActivity(i);
finish();
Finally, I got a solution!
My Context is:- I want disconnect socket connection when activity destroyed, I tried to finish() activity but it didn't work me, its keep connection live somewhere.
so I use android.os.Process.killProcess(android.os.Process.myPid());
its kill my activity and i used android:excludeFromRecents="true"
for remove from recent activity .
Add this attribute to you activity in manifest file.
android:noHistory="true"
You've below options to remove an activity from the back stack for a particular task.
Call finish() method just after startActivity() like this:
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
finish();
Add an Intent flag like so:
startActivity(new Intent(FirstActivity.this, SecondActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY));
Add noHistory=true in your manifest file for that particular activity
You just need to use below code when launching the new activity.
startActivity(new Intent(this, newactivity.class));
finish();
In Kotlin, you may use this
startActivity(Intent(context, newActivity::class.java))
finish()
Or you can use this also
val intent = Intent(context, newActivity::class.java))
startActivity(intent)
finish()