android back key on browser does not launch previous activity - android

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 :)

Related

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.

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>

Open Activity with backwards to MainActivity in my app

I'm stuck with some activity's flow issue. The desired behaviour is the following:
From time to time, the user receives a notification. When this notification is clicked, a new Activity is opened with some information in it. In this Activity, there's a button whose purpose is to redirect the user to another Activity where more detailed information is showed. When the user is in the details Activity and presses the back button (or the back button in the ActionBar) this one is closed and the Main Activity is showed (this one is different from the one I mentioned in first place).
Everything works fine except from the last part. When the user presses the back button the application is closed and it is showed the Home Screen. Why is that happening?
Here is my AndroidManifest.xml:
<activity
android:name=".MainActivity">
</activity>
<activity
android:name=".DetailActivity"
android:label="#string/title_detail_activity"
android:parentActivityName="solar.panik.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="solar.panik.MainActivity" />
</activity>
<activity
android:name=".NotificationActivity"
android:theme="#style/NotificationActivity"
android:excludeFromRecents="true">
</activity>
Here is the onClick code for the button that starts the DetailActivity from the NotificationActivity:
Intent intent = new Intent(NotificationActivity.this, DetailActivity.class);
startActivity(intent);
finish();
Thanks in advance
When you start your app from something other than the launcher, you'll need to pass the back stack with your intent.
Android tutorial
Scroll down to Create Back Stack When Starting Activity().
So in your case:
// Intent for the activity to open when user selects the notification
Intent detailsIntent = new Intent(this, DetailActivity.class);
// Use TaskStackBuilder to build the back stack and get the PendingIntent
PendingIntent pendingIntent =
TaskStackBuilder.create(this)
// add all of DetailActivity's parents to the stack,
// followed by DetailsActivity itself
.addNextIntentWithParentStack(detailsIntent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent);
Check out this answer.
OLD ANSWER BELOW
Make sure in your details activity that onBackPressed() method isn't overridden (or defined).
If that's not it, try adding this to your manifest and remove your current ".MainActivity" Activity and tags. (Or replace it with this)
<activity
android:name="solar.panik.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You have to declare it as MAIN, so the Up button knows where to go. "Back" will take you to the next Activity up on the hierarchy.
Hope that helps.
Do this in your detailed activity. The one you click the back button in.
#Override
public void onBackPressed() {
Intent intent = new Intent(DetailActivity.this, MainActivity.class);
startActivity(intent);
finish();
}

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"

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);
}

Categories

Resources