0

I have one Main Activity that is used as Menu from which a user can go to different Fragments. As switching from one activity to another we do use the Intent for this purpose. And in the same manner we use the onBackPressed() method to go back to the previous Activity. Now my scenario is First I am going from MainActivity (Activity class) to a class that I am extending from Fragment and after that I am going to another fragment (the child fragment of the second). Now I want to go back from that 2nd fragment to the previous fragment but I don't able to achieve although I have tried many things. Just for test I did this

Intent in = new Intent(getActivity(), MainActivity.class) ; 
startActivity(in);
getActivity().finish();

and this brings me to the main Activity. But I want to go back to the previous Fragment. I have tried OnKeyDown but for fragment it shows me errors that onkeydown is undefine for type Fragments. Please give me a simple example that demonstrate the back pressed from one activity to another. I have used this code to go to second Fragment and it brings me well to second fragment but when I press the Back key it shows me a white blank screen, here is my code

Bundle bundle = new Bundle();
                bundle.putSerializable("KEY", dealItem);

                Fragment fragment = new DealCompleteDetailFragment();
                fragment.setArguments(bundle);

                FragmentManager fragmentManager = getActivity()
                        .getSupportFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment)
                        .addToBackStack(null).commit();
                actionBar = MainActivity.myCustomActionBar;
                actionBar.hide();

EDITED:

public class DealsFragment extends Fragment {
    DatabaseService dbService;
    Services service;
    ActionBar actionBar;
    FragmentDealAdapter lazyadapter;
    RefreshableListView mPullRefreshListView;
    MyArrayList<DealItemDetail> dealDetailItem = new MyArrayList<DealItemDetail>();

    public DealsFragment() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            final Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_deals, container,
                false);

        ListView shopList = (ListView) rootView
                .findViewById(R.id.fragment_deal_listview);

        dbService = DatabaseService.getInstance(getActivity()
                .getApplicationContext());
        service = new Services(getActivity());
lazyadapter = new FragmentDealAdapter(getActivity(), dealDetailItem,
                (dealDetailItem.size() > limit) ? limit : dealDetailItem.size());
        shopList.setAdapter(lazyadapter);

        shopList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                DealItemDetail dealItem = dealDetailItem.get(position - 1);

                if (savedInstanceState == null) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("KEY", dealItem);

                    Fragment fragment = new DealCompleteDetailFragment();
                    fragment.setArguments(bundle);

                    FragmentManager fragmentManager = getActivity()
                            .getSupportFragmentManager();
                    getActivity().getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.frame_container, fragment)
                            .addToBackStack(null).commit();
                    actionBar = MainActivity.myCustomActionBar;
                    actionBar.hide();
                }
            }
        });

return rootView;
}

Activity Oncrate:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerListLeft = (ListView) findViewById(R.id.list_slidermenu_left);
        mDrawerListRight = (ListView) findViewById(R.id.list_slidermenu_right);

        navDrawerItemsLeft = new ArrayList<NavDrawerItem>();
        navDrawerItemsRight = new ArrayList<NavDrawerItem>();

        // Left Drawer Items

        navDrawerItemsLeft.add(new NavDrawerItem(Constants.SHOP_SCREEN_TITLE, false, false, false));
        navDrawerItemsLeft.add(new NavDrawerItem(Constants.DEALS_SCREEN_TITLE, false, false, false));


mDrawerListLeft.setOnItemClickListener(new SlideMenuLeftClickListener());

        // setting the nav drawer list adapter
        adapterLeftDrawer = new NavDrawerListAdapter(getApplicationContext(), navDrawerItemsLeft);
        mDrawerListLeft.setAdapter(adapterLeftDrawer);


LayoutInflater inflator=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.header, null);

        imgLeftMenu  = (ImageButton)v.findViewById(R.id.imgLeftMenu);
actionBarTitle = (TextView) v.findViewById(R.id.title_action_bar);



getSupportActionBar().setHomeButtonEnabled(true);

        getSupportActionBar().setDisplayShowTitleEnabled(false);

        getSupportActionBar().setDisplayUseLogoEnabled(false);

        getSupportActionBar().setDisplayShowCustomEnabled(true);

        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#93b636")));

        getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));

        getSupportActionBar().setCustomView(v);

        myCustomActionBar = getSupportActionBar();

        View homeIcon = findViewById(android.R.id.home);

        ((View)homeIcon.getParent()).setVisibility(View.GONE);

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        imgLeftMenu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(mDrawerLayout.isDrawerOpen(mDrawerListLeft)){
                    mDrawerLayout.closeDrawer(mDrawerListLeft);
                } else {
                    if (mDrawerLayout.isDrawerOpen(mDrawerListRight)){
                        mDrawerLayout.closeDrawer(mDrawerListRight);
                    }
                    mDrawerLayout.openDrawer(mDrawerListLeft);
                }
            }
        });

if (savedInstanceState == null) {
            // on first time display view for first nav item
            //          LeftMenuClickListner(0);
        }


private void LeftMenuClickListner(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new ShopsNew();
            break;
        case 1:
            fragment = new DealsNew();
            break;
default:
            break;
        }

if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
            .replace(R.id.frame_container, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerListLeft.setItemChecked(position, true);
            mDrawerListLeft.setSelection(position);
            mDrawerLayout.closeDrawer(mDrawerListLeft);
        }}
Abid Khan
  • 2,451
  • 4
  • 22
  • 45
  • Check that answer posted in that link.`http://stackoverflow.com/questions/24032956/action-bar-back-button-not-working` – Stephen Jul 16 '14 at 13:12
  • you're probably not using `getSupportFragmentManager().beginTransaction().replace(new WhateverFragment(), R.id.main_fragment_container).addToBackStack(null).commit();` – EpicPandaForce Jul 16 '14 at 13:16
  • @Zhuinden I am using this to go to next Fragment and now i am in second Fragment and when I press back button it only show me an empty screen. – Abid Khan Jul 16 '14 at 13:20
  • are you sure you had `.addToBackStack(null)` ? and that you added the first fragment in the same manner as `if(savedInstanceState == null) { getSFM().bT().add(new FirstFragment(), R.id.main_fragment_container).aTBS(null).commit(); }`? – EpicPandaForce Jul 16 '14 at 13:21
  • so how do you create the FIRST fragment? – EpicPandaForce Jul 16 '14 at 13:35
  • I just simple extend it from Fragment and inflate a view for it and there are some click listeners and nothing else there. – Abid Khan Jul 16 '14 at 13:38
  • ....post your entire Activity `onCreate(..)` code. – EpicPandaForce Jul 16 '14 at 13:42
  • @Zhuinden see the code above now. – Abid Khan Jul 16 '14 at 13:48
  • this is the fragment, we need the Activity because that's where the problem is, specifically Activity onCreate() – EpicPandaForce Jul 16 '14 at 13:49
  • this is the first fragment from which i go to second one but any how i am gonna post the activity code too – Abid Khan Jul 16 '14 at 13:52
  • @Zhuinden See now the last updateed code – Abid Khan Jul 16 '14 at 14:00
  • Look at that, `FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment).commit();` is missing .addToBackStack(null); please note that I think if the screen gets rotated it automatically tries creating the zero index Fragment, in which case you would need to call `if(savedInstanceState != null) { onBackPressed(); }` – EpicPandaForce Jul 16 '14 at 14:02
  • @Zhuinden Did what you said but the problem remain unsolved. – Abid Khan Jul 16 '14 at 14:06
  • wait, you added .addToBackStack(null) to that line and it is still not working? o.o? – EpicPandaForce Jul 16 '14 at 14:08
  • Yes see the line FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment) .addToBackStack(null).commit(); – Abid Khan Jul 16 '14 at 14:09

0 Answers0