Android Multiple Activities - android

I have two activities in my app. There is a ListView in the first one and the second one is meant to represent the details for each item of the ListView. The problem is that when I call the second Activity it always opens the same thing but not a new one for each item. Any suggestions?
This is how I changed the calling of the second activity but it is not really working for me.
if (item.getTitle().equals("Open")){
Intent intent = new Intent();
intent.setClass(this, DetailsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}

You need to make that activity singleTop and pass the flag clear top when you are starting it.
in AndroidManifest.xml:
<activity
android:name=".SomeActivity"
android:label="SomeLabel"
android:launchMode="singleTop">
</activity>
in your calling Activity:
Intent i = new Intent();
i.setClass(this, SomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
You could also be finishing the second activity by calling finish(); when the user leaves it.

Related

Android finishAndRemoveTask dosenot start second activity

Android finishAndRemoveTask dosenot start second activity
this is the first activity
Intent i = new Intent(currentAct.this, secondAct.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("abcd", j);
startActivity(i);
finishAndRemoveTask();
If I dont add finishAndRemoveTask(); it works fine the secondAct starts
this is the manifest
<activity
android:name="currentAct"
android:noHistory="true"
android:theme="#style/Theme.AppCompat.Translucent"
android:taskAffinity=".ShortCutActivity">
</activity>
<activity
android:name="secondAct"
android:noHistory="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen"
android:taskAffinity=".ShortCutActivity">
ShortCutActivity is just a mainactivity
how can we start second activity and remove the first activity from stack also
use this before calling startActivity(intent)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
You should add flag FLAG_ACTIVITY_CLEAR_TASK for launching intent.
Intent intent = new Intent(this, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Referring to docs:
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.

Android Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK do not work from Dialog

I don't want user to be able to go somewhere back from my LoginActivity.
This works nice from MainActivity (from Navigation Drawer):
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Result - back stack is cleared.
But when I'm trying to call this from my custom Dialog:
private Context mContext;
Intent intent = new Intent(mContext, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
dismiss();
mContext.startActivity(intent);
((Activity)mContext).finish();
back stack isn't clear, I can go back from my LoginActivity to previous window.
Tried to search the reason, but no result till now.
Try clearing your activity stack:
Set android:noHistory= "true" in your AndroidManifest.xml file in the <activity tag for the login activity.
http://developer.android.com/reference/android/R.styleable.html#AndroidManifestActivity_noHistory
I had a similar problem and adding android:launchMode="singleTop" to my activity in AndroidManifest.xml did the work.
I took the idea from this question

Android Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK not working

I am developing small android application in which I have 3 activities say A1, A2, A3. I can come back from A2 to A1 but when I start A3 and then if user clicks back button then I want to close all previous windows. I tried this in following ways:
Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
but this is not working for me. I am using action-bar activity.I tried to clear task :
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
Above clearing task working for me but it start my A3 with some blank white screen.
Use this
Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); // call this to finish the current activity
try this
Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Have you declared android:launchMode="singleTop" for all activities in the manifest? Also, declare it to MainActivity:
<activity android:name=".MyActivity" android:launchMode="singleTop" ... />
Usually, this behaviour happened when we do not declare a launchMode in the manifest. Although you are using Intent.[MODE], then you will need this attribute. See this documentation for more information about launchMode.

Finish Current Activity and start a new one

I have a Fragment F. After getting result from a service, F needs to finish the activity it is in and launch a page in the web browser which I do using an intent.
It does seem to work fine if the user presses the back button. However, if he launches the app from recent apps, the activity isn't finished.
I have thought about otherways of doing it. Like finishing the current activity and opening the page from the parent activity. But I'll have to make a lot of changes in the flow. So that would be my last option. Is there any way to make sure that the activity is finished even when I launch it from recent apps?
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
getActivity().finish();
Edit: Added code.
try this code for start browser and clear all the stacks of your application
Intent intent = new Intent(); // need to set your Intent View here
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
getActivity().startActivity(intent);
getActivity().finish();
Updated
Try this attribute in your activity in AndroidManifiest.xml file
<activity android:name=".MainActivity"
android:excludeFromRecents="true" ...
use the following code
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this.finish();

Which intent flag do I need to use so that When When I press back I don't see that activity again?

I have an activity that leads to another. i want to make it so that the first activity never opens again once it directs to the other one
clear_top doesnt work. which flag is needed?
This will start a activity with new information and no history
Intent intent = new Intent(MainActivity.this, TargetActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
MainActivity.startActivity(intent);
For your Reference, you can read android documentation : Click Here
You can also use android:noHistory="true" for the first activity in your AndroidManifest.xml. No need for intent flags.
http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
i = new Intent(HomeActivity.this, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
That should do it.
You could just call finish() in your first Activity after your call to startActivity(...).

Categories

Resources