I have an app that has 5 activities (A,B,C,D,E). The app can navigate between those activities. When the user presses home button in device the app goes background and after when the app comes to foreground, first activity should be launched i.e A activity.
Example: app in D activity, after pressing home, the app goes background and when it comes to foreground again it should open A activity not D.
Solutions which i have tried is launch mode, I set the launch mode for A activity (singleInstance) but could not able to find the required solution.
For launch same Activty you Should clear all the Activity when app goes into background.When app goes background use below code that will clear current activity and all other activity that are in stack.
For API 16+, use
finishAffinity();
For lower (Android 4.1 lower), use
ActivityCompat.finishAffinity(YourActivity.this);
When you press Home-Button change to Activity A. Maybe this will work:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_HOME)
{
Intent intent = new Intent(this, MyActivityName.class);
//replace MyActivityName.class with the name of your Activity A
startActivity(intent);
}
return super.onKeyDown(keyCode, event);
}
you may get ondestroy() or onpause() method. on it youcan do
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); //
You can set android:clearTaskOnLaunch="true" for Activity A in your manifest file to achieve what you want:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.beispieldomain.stackoverflowxmlparse">
<application
...
<activity
android:name=".MainActivity"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Have a look here:
Managing Tasks
clearTaskOnLaunch
Related
to prevent the app from reload after clicking on deeplink. I have made the lunch mode for the splash activity singleTask and handled any new intent in onnewintent method
<activity
android:name=".activities.SplashActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.Splash">
</activity>
<activity
android:name=".activities.MainActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
>
</activity>
also i made the maniActivity to be signleTask to prevent reload every time user open deeplink. the splash call onNewIntent smoothly but the mainactivity recreated every time how to make the MainActivity call onNewIntent instead of calling onCreate I have tried to add flags FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP to MainActivity intent but it's not working
//splashActivty
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Intent launchIntent = new Intent(this, MainActivity.class);
startActivity(launchIntent);
}
You need to use setIntent and then continue execution in onResume.
Looking at this link I would say the startActivity might be a problem.
I have added a deep link to my app which maps an activity to a particular web page on my website (Link referred: https://developer.android.com/training/app-links/deep-linking ).
While handling the deep link in my Java code, getAction() and getData() methods give me null value.
I tried testing it here: https://firebase.google.com/docs/app-indexing/android/test (This gave me perfect result) But the same link opens in A web browser rather than in my app when clicked.
Android Manifest code :
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:exported="true">
<tools:validation testUrl="https://www.mywebsite.com/xyz" />
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="www.mywebsite.com"
android:pathPrefix="/xyz" />
</intent-filter>
</activity>
Java Code :
Intent intent = getIntent();
String action = intent.getAction(); // getting null value here
Uri data = intent.getData(); // getting null value here
I want that if the app is present, then it should be opened when the link is clicked or else the link will open the web page.
Where and what am I missing?
You can run into this problem because using singleTask. In that case you should use onNewIntent to 'update' intent, because onCreate and onResume will not handle it.
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the Intent#FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
//TODO: do something with new intent
}
instead of getting intent data in onCreate, get in onResume
#Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
if (intent != null && intent.getData() != null ){
Toast.makeText(this, intent.getData().toString(), Toast.LENGTH_SHORT).show();
webView.loadUrl(intent.getData().toString());
}
}
add these lines of code which activity is attached for deep linking to open your website page
and mark it as a answer if it helped you to solve your problem
My first App
This is my first android app - QR Code Scanner using ZXingScanner Library.
Is there any way to launch a new screen (activity) instead of replacing the crurent one, so i can press back from the scanner to go back to main screen where is the button?
Please help me. Thanks all
I think that this site can help you: http://developer.android.com/training/basics/firstapp/starting-activity.html
In short words:
You need to create new
Intent myIntent = new Intent(this, SecondActivity.class) and than start it by startActivity(myIntent).
Additionally it will be good to define your MAIN activity in AndroidManifest.
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You should create 2 activities in your app.
From first activity send an intent to the second activity that you created before (I think that you can create one). With sending an intent to another activity the first one remain, and you can use back button to load that.
Now this is the easiest way that you can send an intent:
Intent intent= new Intent(Activity1.this,Activity2.class);
startActivity(intent);
But there is some other ways to send an activity(similar to this one):
How to start new activity on button click
My android application has multiple activities. Each activity has a 'Back to Home' button. For example, Main activity opens sub-activity A, sub-activity A opens sub-activity B.
On the screen of sub-activity B, there's a 'Back to Home' button which is supposed to bring back the Main activity into visibility.
I've tried the flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT but no use.
This is a part of my AndroidManifest.xml:
...
<activity android:name="vn.agritrade.Main_Activity"
android:label="Agritrade"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:uiOptions="splitActionBarWhenNarrow"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
...
This is how I relaunch the Main activity:
/**
* Relaunch main activity (called by 'Back to Home' button)
*/
public void open_main_activity(View view) {
Intent intent = new Intent(this,Main_Activity.class);
intent.putExtra(DEFAULT_EXTRA,"");
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
What else should I need?
Use below code it may help,
Intent intentHome = new Intent(getApplicationContext(), MainActivity.class);
intentHome.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentHome.putExtra(DEFAULT_EXTRA,"");
startActivity(intentHome);
It is worked for me.
Don't forget to mark as answer it is help to you.
Intent.FLAG_ACTIVITY_CLEAR_TOP may solve your problem
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
Intent intent = new Intent(this,Main_Activity.class);
intent.putExtra(DEFAULT_EXTRA,"");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
My final solution is:
Keep the main activity stay open
Each time I need to turn back to main screen: close all sub-activities
Lets say I launch my app from the home screen, navigate through some activities, then I press the home key and do something else in the Gmail app.
After i'm done checking my mail,I press the home key again to leave the Gmail app and click my app's icon at the home screen again to return to it.
When I return to my app, I want it to return to the last activity I was at, NOT start a whole new session. I've been trying to figure this out all day.
My manifest for my first activity is as follows:
<activity android:name=".Main"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:alwaysRetainTaskState="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The category attribute LAUNCHER makes my app ALWAYS start at activity Main, so I don't know how to go about restoring the last activity. People have told me to use sharedpreferences to save the last activity and load it on Launch, but I don't think it's intended to be done that way because it isn't very elegant.
I think its the only way because what happens when you're launching an app is that Launcher application sends intent "android.intent.action.MAIN" And the only activity in your app that responds to this intent is your main activity, thus it gets activated. So the only thing you can do is save somewhere your session and on activity start up if there's already saved session restore the app to the previous state.
Try using one of these in your manifest :
<activity android:launchMode=["multiple" | "singleTop" |
"singleTask" | "singleInstance"] ...
Are onResume() and onPause implemented properly?
protected void onResume(){
super.onResume();
}
protected void onPause() {
super.onPause();
}