Android, bring already started activity back to front - java

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

Related

MainAcitivity Does not enter onNewIntent Method and call onCreate Again even it's SingleTask

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.

Why finish() make all the activities can't go back to previous activity?

I'm new in Android Java and I have some question about the intent. This is the intent concept of the app
First, The Launch Activity have two button to let the user push the screen to Register Activity and Login Activity . Other than that, User also can press the back button to go back the Launch Activity. So it is not include finish().
//Login Activity
Intent i = new Intent(LaunchActivity.this, LoginActivity.class);
LaunchActivity.this.startActivity(i);
In the AndroidManifest, Main Activity (yellow) is the first activity.
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
On Main Activity, I will check if whether the user is login to the app or not. If haven't login, I will push the screen to Launch Acitivty by using this code. (I'm using this code on the sign out button too)
Intent i = new Intent(MainActivity.this, LaunchActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
finish();
So I use finish() to prevent the user go back to the Main Activity.
But the problem is the finish() also let the Login and Register Activity can't go back to the Launch Activity.
So is the finish() make all the activities can't go back to previous activity? why and how to fix it?
Due to the comments, I know the problem is the flag. So I removed those flags when checking the login status and sign out. but after I login successfully (login screen to main screen), I can press the back button to go back to the launch screen on the main screen. why? It only happen when first login. after I relaunch the app, the app work fine. This is the code I use on the login button.
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
finish();
Is it because we can't destroy the launch screen on the login screen? but adding those flags should be destroy all the history right?

Start the First screen each time app comes from background in android

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

How to connect two activities to make a program?

I am a beginner in java.
I want to make sure to link my second main activity on my first activity.
I try to put a line of code in my first activity but that does not connect anything at all.
I put the line of code:
Intent intent = new Intent(this, DeuxiemeActivity.class);
startActivity(intent);
finish();
Thanks,
Inside AndroidManifest.xml,
Change
<activity android:name=".MainActivity">
to this,
<activity android:name=".MainActivity" android:noHistory="true">
noHistory="true" and finish() both are similar.
But, When we start a new activity, It will be closed at the same time. So using noHistory="true" is better than using finish();.

Launch a new screen (activity) instead of replacing the crurent one

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

Categories

Resources