Action bar Back button not working

android, android-actionbar

Solution

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

Problem

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: Output: But My problem is After watching the Gallery images I press the `action bar back button`. Then `it is not working`.But it have to `go back to previous page`. Listed below are the codings. GalleryActivity.java: ``` import android.app.ActionBar; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.NavUtils; import android.view.MenuItem; import com.fth.android.R; public class GalleryActivity extends FragmentActivity { private int position; private static String id; private static String name; private DemoCollectionPagerAdapter mDemoCollectionPagerAdapter; private ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gallery); position = getIntent().getExtras().getInt("position"); id = getIntent().getExtras().getString("id"); name = getIntent().getExtras().getString("name"); mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager()); // Set up action bar. final ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); // getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_USE_LOGO|ActionBar.DISPLAY_HOME_AS_UP); // Set up the ViewPager, attaching the adapter. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mDemoCollectionPagerAdapter); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Intent upIntent = new Intent(this, HomeActivity.class); upIntent.putExtra("position", position); if (NavUtils.shouldUpRecreateTask(this, upIntent)) { TaskStackBuilder.from(this) .addNextIntent(upIntent) .startActivities(); finish(); } else { NavUtils.navigateUpTo(this, upIntent); } return true; } return super.onOptionsItemSelected(item); } } ``` GalleryDetailFragment.java: ``` import com.sit.fth.model.GalleryDetail; import com.sit.fth.util.APIServiceHandler; import com.sit.fth.util.AppConstants; import com.sit.fth.util.AppPromoPager; public class GalleryDetailFragment extends BaseFragment implements PromoPagerListener { private TextView countView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { this.setHasOptionsMenu(true); id = getArguments().getString("id"); name = getArguments().getString("name"); View view = inflater.inflate(R.layout.app_pager, null); return view; } } ``` Anybody can help me if you know how to solve these.Thank You.

Original source

Related problems