The correct way to control the up key?

I am using ActionBarSherlock (although I do not think it matters).

I have a main action and activity. I want the "About Activity" function to display the back arrow in its logo and perform the corresponding animation. I do not know how to do it properly.

I currently have onOptionsMenuItemSelected to trigger the main action when I press the Up / Home button, but it is hacked and does not work correctly. It plays incorrect animation and poorly handles multitasking.

How do I configure this correctly?

Here is part of my main activity that launches About:

Intent aboutIntent = new Intent(MainActivity.this, About.class); MainActivity.this.startActivity(aboutIntent); 

Here is my About Activity:

 package com.stevenschoen.test; import android.content.Intent; import android.os.Bundle; import com.actionbarsherlock.app.SherlockActivity; import com.actionbarsherlock.view.MenuItem; public class About extends SherlockActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.about); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // app icon in action bar clicked; go home Intent intentHome = new Intent(this, MainActivity.class); intentHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intentHome); return true; default: return super.onOptionsItemSelected(item); } } } 
+25
java android android-actionbar
Jul 03 '12 at 4:30
source share
6 answers

The root of my problem has been discovered - this is a change in the manifest that I made some time ago: I added this line:

 android:launchMode="singleInstance" 

therefore my main activity will not be resumed. Change it to:

 android:launchMode="singleTask" 

solved my problems and I managed to remove all things onOptionsItemSelected . I knew that something was wrong, that Android should have handled it better, and I was right. Check manifest: P

+12
Jul 03 2018-12-12T00:
source share

You also tried this (taken from the Android website here ):

in the manifest, for each action X that needs to go to the main action, add this to the code:

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar Up/Home button case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } 

and this is his manifest xml tag:

 <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.activities.MainActivity" /> 

if you still need to have the same state in the main action, use this code instead:

 Intent intent = NavUtils.getParentActivityIntent(this); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); NavUtils.navigateUpTo(this, intent); 

if the API is 16 or higher, you can add the parentActivityName attribute with the main path instead of metadata.

+29
Nov 30 '13 at 23:03
source share

In onCreate(Bundle savedInstanceState) do

 ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); 

Then in onOptionsItemSelected(MenuItem item) do

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // go to previous screen when app icon in action bar is clicked Intent intent = new Intent(this, PreviousActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); return true; } return super.onOptionsItemSelected(item); } 
+10
Jul 03 2018-12-12T00:
source share

You can handle the "Action" button using code or XML.

Check this code

if you want it programmatically. Add this line to the onCreate () method

 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

And override this onSupportNavigateUp () method

 @Override public boolean onSupportNavigateUp(){ finish(); return true; } 

OR Not programmatically, you can add a meta to the manifest file as

 <activity android:name="Current Activity" android:parentActivityName="Activity you want to open"> </activity> 

Note. Make sure the action bar doesn't matter.

+8
Jun 08 '16 at 3:31 on
source share

For those who just want to return to their previous activity, this is the simplest solution:

 public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); //this method close current activity and return to previous return true; } return super.onOptionsItemSelected(item); } 
+5
Nov 27 '14 at 16:36
source share

make sure your android: minSdkVersion = "11", which can be seen in the manifest file, the "Up" icon was added from APK 11. I made a small PLZ sample by trying the link below, which can help you simply import into your workspace

http://www.mediafire.com/?hktdvts7yyduwv1

0
Oct 21 '13 at 10:21
source share



All Articles