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.
Related
I am developing a calling application. My HomeActivity is a singleTask activity. My call activity is also a singleTask activity.
From HomeActivity a call is initiated. At this moment, there are two tasks for my application as they both are singleTask. In call screen I have a button to reach my HomeActivity.
When I press the home button in my call screen and navigate back, my call activity is destroyed. But it should not get destroyed. It should remain.
When I press home button in call screen I do the below.
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setClass(this.getActivity(), MyHomeActivity.class);
startActivity(intent);
My manifest declaration:
<activity
android:name=".XXX.MyHomeActivity"
android:label="#string/app_name"
android:alwaysRetainTaskState="true"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustNothing"
android:theme="#style/MyTheme"
>
</activity>
<activity
android:name=".XXX.MyCallActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="#style/MyTheme"
android:windowSoftInputMode="adjustResize">
Inflating HomeActivity from CallActivity:
Intent intent = new Intent();
intent.setClass(this.getActivity(), HomeActivity.class);
startActivity(intent);
Inflating CallActivity:
Intent intent = new Intent();
intent.setClass(this, CallActivity.class);
startActivity(intent);
Can anyone help me with the navigation parameters to use to achieve this?
In order to have 2 tasks, you need to make sure that the root Activity of the tasks have different taskAffinity, otherwise Android will put both in the same task. The default taskAffinity is the name of your package.
Add android:taskAffinity="" to one of the <activity> declarations.
According to the documentation using FLAG_ACTIVITY_CLEAR_TASK will do the following:
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.
So if want your CallActivity to stay in the back stack you can simply do this:
Intent intent = new Intent();
intent.setClass(this.getActivity(), MyHomeActivity.class);
startActivity(intent);
I have LoginActivity where after successful logging I start MainActivity via intent and finish LoginActivity.
I press back button and then open app via icon and It shows me MainActivity but if I open app from recent apps list after pressing back button so I see LoginActivity
I've checked if LoginActivity was destroyed
How can It be?
manifest
<activity android:name=".LoginActivity" />
<activity android:name=".MainActivity"
android:launchMode="singleTask" />
start MainActivity
Intent intent = new Intent(getActivity(), MainActivity.class);
mProgressDialog.dismiss();
startActivity(intent);
getActivity().finish();
Remove android:launchMode="singleTask"
Why you are adding launchMode, adding this you will be able to get its instance only once. Let default be "Standard", for more information, please have a look at the documentation.
Docs say:
The "standard" and "singleTop" modes differ from each other in just one respect: Every time there's a new intent for a "standard" activity, a new instance of the class is created to respond to that intent. Each instance handles a single intent. Similarly, a new instance of a "singleTop" activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created.
So, you wouldn't need singleTask launch mode. Apart from this, I can't see <intent-filter> for your LoginActivity as being MAIN action and LAUNCHER category.
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.
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.
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" />