Android - Back button in the title bar

前端 未结 26 2068
南方客
南方客 2020-12-04 07:02

In many apps (Calendar, Drive, Play Store) when you tap a button and enter a new activity, the icon in the title bar turns into a back button, but for the app I am making, i

相关标签:
26条回答
  • 2020-12-04 07:20

    You can also simply put onBackPressed() in your onClick listener. This causes your button to act like the default "back/up" buttons in android apps!

    0 讨论(0)
  • 2020-12-04 07:22

    If your activity extends AppCompatActivity you need to override the onSupportNavigateUp() method like so:

    public class SecondActivity extends AppCompatActivity {
    
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_second);
           Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
           setSupportActionBar(toolbar);
           getSupportActionBar().setHomeButtonEnabled(true);
           getSupportActionBar().setDisplayHomeAsUpEnabled(true);
           ...
       }
    
       @Override
       public void onBackPressed() {
           super.onBackPressed();
           this.finish();
       }
    
       @Override
       public boolean onSupportNavigateUp() {
           onBackPressed();
           return true;
       }
    }
    

    Handle your logic in your onBackPressed() method and just call that method in onSupportNavigateUp() so the back button on the phone and the arrow on the toolbar do the same thing.

    0 讨论(0)
  • 2020-12-04 07:25

    There are two simple steps to create a back button in the title bar:

    First, make the application icon clickable using the following code in the activity whose title bar you want to have a back button in:

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

    After you have added the above code, you will see a back arrow appear to the left of the application icon.

    Second, after you have done the above, you still have to create code that will take advantage of the click event. To do so, be aware that, when you actually click on the application icon, an onOptionsItemSelected method is called. So to go back to the previous activity, add that method to your activity and put Intent code in it that will return you to the previous activity. For example, let's say the activity you are trying to go back to is called MyActivity. To go back to it, write the method as follows:

    public boolean onOptionsItemSelected(MenuItem item){
        Intent myIntent = new Intent(getApplicationContext(), MyActivity.class);
        startActivityForResult(myIntent, 0);
        return true;
    }
    

    That's it!

    (In the Android developers API, it recommends messing around with the manifest and adding stuff like android:parentActivityName. But that doesn't seem to work for me. The above is simpler and more reliable.)

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

    And in your Activity

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    0 讨论(0)
  • 2020-12-04 07:25

    Light-weighted version without using ActionBarActivity that still has the same bahaviors here:

    public class ToolbarConfigurer implements View.OnClickListener {
        private Activity activity;
    
        public ToolbarConfigurer(Activity activity, Toolbar toolbar, boolean displayHomeAsUpEnabled) {
            toolbar.setTitle((this.activity = activity).getTitle());
            if (!displayHomeAsUpEnabled) return;
            toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
            toolbar.setNavigationOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            NavUtils.navigateUpFromSameTask(activity);
        }
    }
    

    Usage: Put new ToolbarConfigurer(this, (Toolbar) findViewById(R.id.my_awesome_toolbar), true); in onCreate.

    0 讨论(0)
  • 2020-12-04 07:25

    It can also be done without code by specifying a parent activity in app manifest If you want a back button in Activity B which will goto Activity A, just add Activity A as the parent of Activity B in the manifest.

    0 讨论(0)
  • 2020-12-04 07:27

    1.- Add the activity to AndroidManifest.xml and make sure to provide the meta-data:

    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
    

    2.- Add the following code to the onCreate method of the activity:

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

    3.- Override the onOptionsItemSelected and use NavUtils.navigateUpFromSameTask() static method to navigate throw the stack.

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

    However, using navigateUpFromSameTask() is suitable only when your app is the owner of the current task (that is, the user began this task from your app). If that's not true and your activity was started in a task that belongs to a different app, then navigating Up should create a new task that belongs to your app, which requires that you create a new back stack.

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