Up Navigation and singleTop launch mode - android

I have an activity A, and when I press an toolbar item, it starts activity B using startActivity(intent). Whenever I press back button or the up navigation icon, it closes my app. I believe it's because I'm using launchMode="singleTop" in my parent activity (I'm using this because I have a Search View and a searchable configuration, because I don't want to start another instance of my activity on for search). So the question is: How can I get back from child activity(B) to parent activity(A) using both up navigation and back button without closing my app? I've searched about it, and I found something about onNewIntent(). If this is my solution, how should I use it properly?
Here is my manifest file:
<activity
android:name="com.example.fernando.inspectionrover.MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name="com.example.fernando.inspectionrover.BluetoothSettingsActivity"
android:parentActivityName="com.example.fernando.inspectionrover.MainActivity"
android:screenOrientation="landscape">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.fernando.inspectionrover.MainActivity" />
Here is how start my new activity:
switch (id) {
case R.id.bluetoothActivity:
Intent switchActivity = new Intent(this, BluetoothSettingsActivity.class);
startActivity(switchActivity);
Log.i(LIFE_CYCLE, "Switching from " + getLocalClassName() + " to Bluetooth Setting Activity");
finish();
break;
}

Single Top means that if you launch an activity that is already on top, it wont be created again just resumed.
The reason your back navigation close the app is because you are calling finish() just after you start a new activity. Which means you don't need that activity anymore so it is removed from the stack. If you go back on activityB, the app will close because there is nothing to go back (you called finish() remember?

I may be just poking at the easiest answer but I think the main problem is that you are calling finish after you start the new activity. This calls on destroy for the calling activity and removes it from the activity stack.

Related

Launcher development - Home button not going back to initial activity

I have an app that acts as a Launcher. This app has 3 activities:
SplashActivity: shows a splash screen while loading, then launches LauncherActivity and finishes. This is the Activity marked as launcher in the manifest.
startActivity(Intent(this, LauncherActivity::class.java))
finish()
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
LauncherActivity: main activity for Launcher. Has a menu button that launches DashboardActivity.
startActivity(Intent(this#LauncherActivity, DashboardActivity::class.java))
<activity
android:name=".LauncherActivity"
android:launchMode="singleTask"
android:screenOrientation="landscape" />
DashboardActivity: shows a list of apps and launches them through their launch intent.
private val DEFAULT_FLAGS_APP_LAUNCH = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(packageManager.getLaunchIntentForPackage(packageInfo.packageName).apply {
flags = DEFAULT_FLAGS_APP_LAUNCH
})
<activity
android:label="#string/apps"
android:theme="#style/TNA"
android:name=".DashboardActivity"
android:launchMode="singleTask"
android:screenOrientation="landscape" />
All activities are launched through startActivity, including the apps.
I want the standard Android Launcher behavior, that is: when entering an app through DashboardActivity, if I click home button, go to the main Launcher activity (LauncherActivity), and when clicking back, go to the dashboard (DashboardActivity).
The problem I have is that when clicking home, it goes back to DashboardActivity, not to LauncherActivity. If I finish DashboardActivity, then when clicking back on an app, it goes back to LauncherActivity.
Any ideas on how to solve this?
This is definitely back/task stack related. See this link for more information about the task stack.
When you go from LauncherActivity to DashboardActivity, the dashboard is placed on to the task stack. When the LauncherActivity is requested again via the HOME button, the task stack is restored back to the last Activity which was in use after launching the LauncherActivity, which was DashboardActivity.
You have several different options to resolve this:
Don't use a separate Activity for the "dashboard". Consider a drawer or even a Fragment which shows the content and can be popped back to the main LauncherActivity when it is done calling startActivity to launch another app.
After your DashboardActivity calls startActivity, it should call finish() so it will get popped off the current task stack.
Usually launchers are setup to be launched in singleInstance mode, preventing multiple instances of the launcher Activity to run at the same time. Note that you'll need to support onNewIntent in your LauncherActivity.
To prevent odd interactions with the task manager, consider setting FLAG_ACTIVITY_NO_HISTORY when launcher your DashboardActivity.

I have to press back twice to exit my app

I have tried everything I have been able to find online to stop this happening before someone suggests 'Just go google it'....
I will be explicit in case it is not clear enough, I do not want to have to click back twice when I am on the registration or login screen. Its like I have an Activity A the user never sees, A starts an intent to either B or C.
On B or C I do not want to click twice to exit the application.
I have an Activity that I want to be invisible to the user, its purpose is to check if the application has been registered, if it has it starts an intent to login Activity, if the app is not registered it starts an intent to registration activity. The issue that when on the login or registration screen I have to press back twice to exit the application.
If I do not call finish() in this Activity the back button takes me to the transparent Activity and I have to press back again, calling finish() means I don't go back to the transparent Activity but I do have to press back twice - which I don't want.
I already tried calling the startActivityForResult() example, but this doesn't have any effect.
The Activity is using the transparent theme as using the NoDisplay theme caused an exception, googling this seemed to imply an issue with the emulator and suggested fix was to use transparent.
<activity
android:name=".activity.AppEntryPoint"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The Activity doesn't do a lot at the moment, currently I am trying the code below but this also has no effect and I still have to press the back button twice.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!"".equals(((GlobalData) this.getApplication()).password)) {
navigateToLoginScreen();
} else {
registerApplication();
}
}
private void registerApplication() {
Intent registerScreen = new Intent(this, RegistrationActivity.class);
registerScreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(registerScreen);
finish();
}
private void navigateToLoginScreen() {
Intent loginScreen = new Intent(this, LoginActivity.class);
loginScreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginScreen);
finish();
}
What can I do to make sure that the user only has to press back once when they get to the second screen, whether it is the login or the registration screen?
i think this solution will solve your problem.
just add android:noHistory="true" in the manifest file to your first activity, like this:
<activity
android:name=".activity.AppEntryPoint"
android:label="#string/app_name"
android:noHistory="true"
android:theme="#android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

How open not main activity from Notification

In my app I have a Activity that is opened from a Notification, but is not declared has main activity or launcher activity·
Also, this activity is declared has "singleTop".
Works fine for most users, but some are a little problem.
In some occassions when click the Notification the Activity is opened and, when click back, the app Main Activity is shown.
How to don't show the main activity when click back?
Also, if the users are in this activity and click home, and later it clicks into the app icon in order to open the Main activity, the previously opened activity, that is not main activity is opened. This can change?
Thanks.
-- The structure of the manifest is:
<activity android:name=".MainActivity" android:label="#string/app_name" android:exported="true" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ShowAlarmsActivity" android:launchMode="singleTop" android:exported="true" android:theme="#style/AppThemeWithoutActionBar" />
you may do this my either overriding on back press and starting the parent activity using this code
or if you are using action bar you may use parent activity tag
Intent intent = new Intent(getApplicationContext(), ParentActivity.class);
startActivity(intent);
also if you want to distinguish that if the user is coming from notification or from app launch you may use intent to pass some key value and cater it
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="parentActivity" />

Android main activity does not launch on start

The main activity does not launch when I open the application; I close it by clicking on the home button. When the app starts, it opens on the same page where I was when I clicked the home button. It somehow remembers the last page I was at, and opens that page instead of the launcher activity. I added onDestroy() to onPause() on all the activities, but that didn't help either.
I am not sure what code to add here, so I'm attaching my AndroidManifest. Thanks! Below are the three activities in the AndroidManifest.
<activity android:name="example.Activity1" android:label="MyApp"
android:configChanges="orientation" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="example.Activity2"
android:configChanges="orientation"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="example.Activity3"
android:configChanges="orientation"
android:screenOrientation="portrait" >
</activity>
When Home button is pressed, onStop method is called in your activity. So what you may do is to add finish(); in onStop method to destroy your activity.
Your activity is saved on the backstack. Play with the flags passed to the intent when you create your activity. For example, FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET will clear all the activities from this to up the backstack.
Intent myIntent = new Intent(this, MyActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(myintent);
When you close, o exit myintent Activity, when you reopen you app it will clear delete myintent Activity from the backstack and all the activities opened from this activity.
Make sure you activity name includes you entire package name instead of example.
Or just replace android:name="example.Activity1" with android:name=".Activity1"

android back key on browser does not launch previous activity

I'm expecting quite a strange behavior in a tiny app I'm currently working on.
The app consists of two activities. From the first activity I'm launching the webbbrowser via an intent. When I press the back key in the browser it returns to the SECOND activity even if I manually closed the app previously before launching.
1) First Activity
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mWebShopURL));
startActivity(intent);
First Activity launches second activity like this
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
Bundle b = new Bundle();
b.putString("product", mProduct);
intent.putExtras(b);
startActivity(intent);
2) Second Activity -> first activity
onBackPressed();
AndroidManifest
<activity
android:name=".FirstActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
If you want the second activity to complete and go away when the user taps on the simple "back button" you refer to in your comment, then don't call onBackPressed(). Please just call finish(). That will make the second activity go away and return the user to the first activity.
Your problem is that you've never removed the second activity from the stack, which is why returning from the browser shows that activity.
If i am correct You are overriding onBackPressed() in your second Activity.Dont do that put onBackPressed() code in comments and try. Hope it will help :)

Categories

Resources