Action bar Back button not working

前端 未结 13 1763
再見小時候
再見小時候 2020-12-01 03:13

with the help of these Android Docs.I am trying to do a action bar Back button.I get an Action Bar Back Button like these below image:

相关标签:
13条回答
  • 2020-12-01 03:50

    Try like

    First of all you need to use addToBackStack() before commit() for Fragments

    @Override
         public boolean onOptionsItemSelected(MenuItem item) {
             switch (item.getItemId()) {
             // Respond to the action bar's Up/Home button
             case android.R.id.home:
                 if(getSupportFragmentManager().getBackStackEntryCount()>0)
                    getSupportFragmentManager().popBackStack();
                 return true;
             }
             return super.onOptionsItemSelected(item);
         } 
    
    0 讨论(0)
  • 2020-12-01 03:51

    Please read this

    you should have something like this:

        <activity
            android:name="com.sit.fth.activity.HomeActivity"
            android:screenOrientation="portrait">
    
        </activity>
        <activity
            android:name="com.sit.fth.activity.GalleryActivity"
            android:screenOrientation="portrait"
            android:parentActivityName="com.sit.fth.activity.HomeActivity">
    
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.sit.fth.activity.HomeActivity"/>
    
       </activity>
    


    then calling NavUtils.navigateUpFromSameTask(this) will cause navigating to parent activity (HomeActivity).

    0 讨论(0)
  • 2020-12-01 03:51

    In my case I had overridden the onCreateOptionsMenu method and I forgot to call super at the end.

    0 讨论(0)
  • 2020-12-01 03:54

    You need to call setDisplayHomeAsUpEnabled(true) method in the onCreate method and override onSupportNavigateUp() and call onBackPressed() in it as below. That's it. done :)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    
    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
    
    0 讨论(0)
  • 2020-12-01 03:54

    Here is one more thing to check for in case the other answers here (or here or here or here) don't work.

    I had copied some code from another activity that disabled the menu. Deleting this method (and applying the solutions given in the others answers) allowed the up button to work.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // hide the menu
        return false;
    }
    
    0 讨论(0)
  • 2020-12-01 03:56

    I solved these problem by adding the below coding in GalleryActivity.

    ActionBar actionBar;
    actionBar=getActionBar();
    
    actionBar.setDisplayHomeAsUpEnabled(true);
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) { 
            switch (item.getItemId()) {
            case android.R.id.home: 
                onBackPressed();
                return true;
            }
    
        return super.onOptionsItemSelected(item);
    }
    

    In MainActivity:

    Previously,

    public class HomeActivity extends BaseActivity

    Then I change into

    public class HomeActivity extends FragmentActivity

    In GalleryFragment:

    I use Intent to pass it to the GalleryActivity.

    @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Gallery gallery = (Gallery) arg0.getAdapter().getItem(arg2);
    
            Intent intent = new Intent(getActivity(), GalleryActivity.class);
            intent.putExtra("position", position);
            intent.putExtra("id", gallery.getGalId());
            intent.putExtra("name", gallery.getAlbumTitle());
            startActivity(intent);
    
            // mCallback.OnGalItemSelected(gallery.getGalId(),gallery.getAlbumTitle());
        } 
    
    0 讨论(0)
提交回复
热议问题