How to implement the Android ActionBar back button?

后端 未结 12 2032
天涯浪人
天涯浪人 2020-11-29 15:38

I have an activity with a listview. When the user click the item, the item \"viewer\" opens:

List1.setOnItemClickListener(new OnItemClickListener() {
    @Ov         


        
相关标签:
12条回答
  • 2020-11-29 16:03

    Following Steps are much enough to back button:

    Step 1: This code should be in Manifest.xml

    <activity android:name=".activity.ChildActivity"
            android:parentActivityName=".activity.ParentActivity"
            android:screenOrientation="portrait">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".activity.ParentActivity" /></activity>
    

    Step 2: You won't give

    finish();
    

    in your Parent Activity while starting Child Activity.

    Step 3: If you need to come back to Parent Activity from Child Activity, Then you just give this code for Child Activity.

    startActivity(new Intent(ParentActivity.this, ChildActivity.class));
    
    0 讨论(0)
  • 2020-11-29 16:05

    AndroidManifest file:

        <activity android:name=".activity.DetailsActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="br.com.halyson.materialdesign.activity.HomeActivity" />
        </activity>
    

    add in DetailsActivity:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    

    it's work :]

    0 讨论(0)
  • 2020-11-29 16:07

    https://stackoverflow.com/a/46903870/4489222

    To achieved this, there are simply two steps,

    Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"

    example :

     <activity
        android:name=".home.ActivityDetail"
        android:parentActivityName=".home.HomeActivity"
        android:screenOrientation="portrait" />
    

    Step 2: in ActivityDetail add your action for previous page/activity

    example :

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
          case android.R.id.home: 
              onBackPressed();
              return true;
       }
       return super.onOptionsItemSelected(item);}
    }
    
    0 讨论(0)
  • 2020-11-29 16:10
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    

    in onCreated method for the new apis.

    0 讨论(0)
  • 2020-11-29 16:11

    Make sure your the ActionBar Home Button is enabled in the Activity:

    Android, API 5+:

    @Override
    public void onBackPressed() {
         ...
         super.onBackPressed();
    }
    

    ActionBarSherlock and App-Compat, API 7+:

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        ...
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    

    Android, API 11+:

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        ...
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    

    Example MainActivity that extends ActionBarActivity:

    public class MainActivity extends ActionBarActivity {
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Back button
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home: 
                // API 5+ solution
                onBackPressed();
                return true;
    
            default:
                return super.onOptionsItemSelected(item);
            }
        }
    }
    

    This way all the activities you want can have the backpress.

    Android, API 16+:

    http://developer.android.com/training/implementing-navigation/ancestral.html

    AndroidManifest.xml:

    <application ... >
        ...
        <!-- The main/home activity (it has no parent activity) -->
        <activity
            android:name="com.example.myfirstapp.MainActivity" ...>
            ...
        </activity>
        <!-- A child of the main activity -->
        <activity
            android:name="com.example.myfirstapp.DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.example.myfirstapp.MainActivity" >
            <!-- The meta-data element is needed for versions lower than 4.1 -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstapp.MainActivity" />
        </activity>
    </application>
    

    Example MainActivity that extends ActionBarActivity:

    public class MainActivity extends ActionBarActivity {
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Back button
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:11

    Android Annotations:

    @OptionsItem(android.R.id.home)
    void homeSelected() {
        onBackPressed();
    }
    

    Source: https://github.com/excilys/androidannotations

    0 讨论(0)
提交回复
热议问题