Android android:parentActivityName go to previous Activity - android

When I press the arrow back I want to navigate to my previous screen(where I came from). So if I'm in MainActivity and go to setting I want the arrow to point back to MainActivity, but if I'm in another Activity I want it to go back to that activity.
<activity
android:name=".SettingsActivity"
android:label="#string/action_settings"
android:parentActivityName=".MainActivity" >
</activity>

How do you start settings activity?
Just use startActivity(intent) in main activity and don't call finish() method. It should be handled automatically.
sample code:
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
just comment what's your problem exactly and I'll edit my answer.

Related

Android: Finish all activity and take user to home without loading home

There is home icon on app's app bar at top and I want to finish all activities and take user to home page. But, I don't want home page to be re-created.
I have already tried following but it re-creates home page and I don't want it
fun goToHomePage() {
val homeIntent = Intent(activity, HomeActivity::class.java)
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)
}
<activity
android:name="HomeActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar"/>
Re-creating home put pressure on our backend and we don't want that. How to do achieve it?
For HomeActivity use "singleTop" launchmode.
In AndroidManifest:
<activity
android:launchMode="singleTop"
android:name="HomeActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar" />
Reference: https://developer.android.com/guide/topics/manifest/activity-element#lmode
Edit:
From the docs:
If an instance of the activity already exists at the top of the target
task, the system routes the intent to that instance through a call to
its onNewIntent() method, rather than creating a new instance of the
activity.
Use onNewIntent() to handle your scenario.
To return to the existing instance of HomeActivity, just do this:
val homeIntent = Intent(activity, HomeActivity::class.java)
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActivity(intent)
This will clear all other activities from the stack back to the existing instance of HomeActivity.
HomeActivity.onNewIntent() will be called.

Android prevent back button from closing the app

In my app, I want to prevent the user from going back to login activity after logging in, but I don't want to close the app. For example if the previous activity is login activity, I don't want to do anything when the user presses back, just stay in the current activity/fragment.
add finish(); in login activity and add onBackPressed in your next activity
#Override
public void onBackPressed() {
}
Add android:noHistory="true" in the manifest of your LoginActivity
<activity android:name=".LoginActivity" android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
The correct way to do this is to call finish() in the Login activity when it completes and launches the next activity. For example:
Intent intent = new Intent(LoginActivity.this, NextActivity.class);
startActivity(intent);
finish();
This would prevent the user from going back from NextActivity to LoginActivity since calling finish() destroys LoginActivity and removes it from the back stack.
There is no need to remove onBackPressed from NextActivity, and that may have unintended consequences (for example, if they navigate from Login -> Next -> Other -> Next then click back, they would expect to go back to Other). Disabling onBackPressed in Next would prevent that.

Android: make sure that specific activity retains in backstack

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);
}

Activity is getting created again instead of resuming and using instance from stack?

I have an android application with several activities .Each and every activity has Application Icon in action bar which helps user to return back to main activity directly instead of pressing back button.My problem is that when I use the icon to start my home activity it does not uses the previous instance from the stack and start creating it again.
My Action bar app icon code is :
startActivity(new Intent(this, DashBoard.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
this above code starts Dashboard activity and calls its both onCreate() and onResume().But If I uses back button to return to this activity from any activity it just calls onResume().
Activity definition from manifest file:
<activity
android:name=".DashBoard"
android:configChanges="keyboardHidden"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
Why is this happening?Am I missing something to prevent it from not creating it again?Please help
Thanks
Use setFlags(), instead of addFlags(). You are on right track. Use the following code.
Intent intent = new Intent(this, DashBoard.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
Remove the FLAG_ACTIVITY_CLEAR_TOP.

Android Activity lifecycle conflict

I've an app with 3 activities
LoginActivity
ActivityA
ActivityB
User starts with LoginActivity. After successful login, he goes to ActivityA. ActivityA invokes ActivityB using startActivityForResult and processes the response using onActivityResult.
If user presses 'Home' button from ActivityA or ActivityB and relaunches the application, I want to take user back to LoginActivity
I tried playing around with onRestart and onResume. Both of these are called when ActivityA is reinitialized via home screen or onActivityResult (when user comes back from ActivityB).
How can I implement this requirement?
PS: I checked similar questions on SO and did not find something that matches my requirement.
Thanks.
I will redirect you to this question here. I think it should answer your question.
When you declare your intents to start activity A and activity B, try using the nohistory flag e.g.
Intent intent = new Intent(this, ActivityA.class);
// do not keep this intent in history
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
Then when you relaunch, it will go back to the login activity.
Try writing this in your AndroidManifest file for each activity
<activity android:launchMode="singleTask"
android:name=".LoginActivity" />
<activity android:launchMode="singleTask"
android:name=".ActivityA" />
<activity android:launchMode="singleTask"
android:name=".ActivityB" />

Categories

Resources