I have to press back twice to exit my app - android

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>

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.

Up Navigation and singleTop launch mode

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.

Opening Android app from URL does not open the right activity

I have a very strange problem regarding Android intent-filter from URL.
In short: I have activity A as MAIN & LAUNCHER and activity B that has intent-filter that allows it to be opened via href="intent://...". Activity B has activity A listed as parent.
If I open the app via clicking the href, it opened directly to B (as expected). But then, if I press back (so I am in activity A) and try to open the app from href again, it opened to activity A instead of B! This is is really strange as activity A does not have any intent filter other than MAIN and LAUNCHER.
What I have right now:
Manifest containing activity A and activity B:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.app">
<activity
android:name=".AActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:parentActivityName=".AActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".AActivity" />
<intent-filter>
<data
android:host="m.something.com"
android:path="/somepath"
android:scheme="https" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
The href I used:
href="intent://m.something.com/somepath#Intent;scheme=https;package=com.my.app;S.extra_string=name#domain.com;end"
Activity B code:
At first, I didn't override the onOptionsItemSelected. I thought if I have specified the parent activity in the manifest, then Android (by default) will open Activity A, but when I opened activity B from URL and press up, it closed the app. Therefore, I need to do the following code.
#Override
public void onCreate(Bundle savedInstanceState) {
setSupportActionBar(...);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (isTaskRoot()) {
Intent upIntent = NavUtils.getParentActivityIntent(this);
startActivity(upIntent);
}
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
Fragment Z code inside activity B:
//Code omitted, basically I get the extra_string from the intent
//(from the a href) and processed it here.
Now, these steps work:
Open app from launcher (hence, activity A is shown).
Tap device's recent app button, open chrome, navigate to web that contains that a href and click it.
App opened activity B, fragment Z can obtain the extra_string
Tap device's recent app button, go to chrome, and click a href again.
App opened activity B, fragment Z can obtain the extra_string.
BUT, these are the steps that lead to the problem:
Swipe app from recent app (this is important).
Open chrome, navigate to web that contains the a href and click it.
The app opened directly to activity B, the fragment Z can obtain the extra_string successfully.
Press up, the app "goes back" to activity A successfully.
Press device's recent app button, open chrome (back to the web that contains a href) and press the a href again.
Expectation:
The app opened directly to activity B.
Reality:
The app opened to activity A.
I really have no idea what's going on here. I have tried to search for similar problem in SO to no avail. Anybody experienced the same problem?

Return back to MainActivity from another activity

The MainActivity contains some buttons. Each button opens a new activity via an intent. These activities then have a button to return to the MainActivity via an intent.
But when I press a button to return to the MainActivity, I get some sort of menu on the screen! Someone who knows what could be wrong? Preciate some help! Thanks!
EDIT: The return button in one of the other activities:
Button btnReturn1 = (Button) findViewById(R.id.btnReturn1);
btnReturn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent returnBtn = new Intent("android.intent.action.MAIN");
startActivity(returnBtn);
}
});
The Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kullaberg.test02"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity1"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.ACTIVITY001" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Activity2"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.ACTIVITY002" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Activity3"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.ACTIVITY003" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
why don't you call finish();
when you want to return to MainActivity
btnReturn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
Here's why you saw the menu with the code you listed in your onClick method:
You were creating an Intent with the constructor that takes a string for the action parameter of the Intent's IntentFilter. You passed "android.intent.action.MAIN" as the argument to that constructor, which specifies that the Intent can be satisfied by any Activity with an IntentFilter including <action="android.intent.action.MAIN">.
When you called startActivity with that Intent, you effectively told the Android OS to go find an Activity (in any app installed on the system) that specifies the android.intent.action.MAIN action. When there are multiple Activities that qualify (and there are in this case since every app will have a main Activity with an IntentFilter including the "android.intent.action.MAIN" action), the OS presents a menu to let the user choose which app to use.
As to the question of how to get back to your main activity, as with most things, it depends on the specifics of your app. While the accepted answer probably worked in your case, I don't think it's the best solution, and it's probably encouraging you to use a non-idiomatic UI in your Android app. If your Button's onClick() method contains only a call to finish() then you should most likely remove the Button from the UI and just let the user push the hardware/software back button, which has the same functionality and is idiomatic for Android. (You'll often see back Buttons used to emulate the behavior of an iOS UINavigationController navigationBar which is discouraged in Android apps).
If your main activity launches a stack of Activities and you want to provide an easy way to get back to the main activity without repeatedly pressing the back button, then you want to call startActivity after setting the flag Intent.FLAG_ACTIVITY_CLEAR_TOP which will close all the Activities in the call stack which are above your main activity and bring your main activity to the top of the call stack. See below (assuming your main activity subclass is called MainActivity:
btnReturn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
)};
use
Intent returnBtn = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(returnBtn);
make the main activity's launchmode to singleTask in Android Manifest if you don't want to create new one every time.
android:launchMode="singleTask"
I'm used it and worked perfectly...
startActivity(new Intent(getApplicationContext(),MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
because Finish() use for 2 activities, not for multiple activities
This usually works as well :)
navigateUpTo(new Intent(getBaseContext(), MainActivity.class));
instead of starting MainActivity again via startActivity, call finish() instead in the other activities to get back to MainActivity... as MainActivity is already in stack
Use this code on button click in activity and When return back to another activity just finish previous activity by setting flag in intent then put only one Activity in the Stack and destroy the previous one.
Intent i=new Intent("this","YourClassName.Class");
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
I highly recommend reading the docs on the Intent.FLAG_ACTIVITY_CLEAR_TOP flag. Using it will not necessarily go back all the way to the first (main) activity. The flag will only remove all existing activities up to the activity class given in the Intent. This is explained well in the docs:
For example, consider a task consisting of the activities: A, B, C, D.
If D calls startActivity() with an Intent that resolves to the component of
activity B, then C and D will be finished and B receive the given Intent,
resulting in the stack now being: A, B.
Note that the activity can set to be moved to the foreground (i.e., clearing all other activities on top of it), and then also being relaunched, or only get onNewIntent() method called.
Source
I just do this way
public void retorna(View view)
{
Intent muda = new Intent(getApplicationContext(),MainActivity.class);
muda.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(muda);
}

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