Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

android - Action bar Back button not working

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:

enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...