4

My question might be described much more better with this image. When I am adding second fragment(2nd image) over first fragment (1st image) first fragment element(Spinner, EditText) are still active.Active mean touching on the same place I can see the dropdown is coming down.

I can't replace the fragment because I need to go back in the same state where user left the first fragment.Can any one tell me what's the problem.

I am adding the second fragment using a broadcast because it need to call from a baseAdapter listview.Code is like this.

ListView onclick

view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) 
        {
            Log.v("TAG", "Clicked: " + itemListPogo.get(position).getitemIdplato());

            Intent i = new Intent("start.fragment.action");
            i.putExtra("plateId", itemListPogo.get(position).getitemIdplato());
            mContext.sendBroadcast(i);
        }
    });

Reveiving

    mBroadcastReceiver = new BroadcastReceiver() 
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            Bundle extra = intent.getExtras();
            String plateId = extra.getString("plateId");  

            Fragment fragment = new PlateDetailsFragment(DashBoardActivity.this, Integer.parseInt(plateId));
            FragmentManager fragmentManager = (DashBoardActivity.this).getFragmentManager();
            fragmentManager.popBackStack("back", FragmentManager.POP_BACK_STACK_INCLUSIVE);

            fragmentManager.beginTransaction().add(R.id.frame_container, fragment).addToBackStack(plateId).commit();
        }
    };
    this.registerReceiver(mBroadcastReceiver, new IntentFilter("start.fragment.action"));

First two pic is first two fragment 3rd image is having the problem

A J
  • 4,542
  • 5
  • 50
  • 80
  • "I can't replace the fragment because I need to go back in the same state where user left the first fragment" -- you seem to be removing the old fragment via `popBackStack()`, so I do not understand this comment. "I am adding the second fragment using a broadcast because it need to call from a baseAdapter listview" -- **please** do not use system broadcasts for this. Use an in-process event bus (`LocalBroadcastManager`, greenrobot's EventBus, Square's Otto, etc.). As it stands, anyone can hack your app. – CommonsWare Feb 29 '16 at 11:58

2 Answers2

7

There is a simple trick for this. Take the main container/ the parent view of your top fragment and add an onClickListener for it. Don't do anything inside onClick. This way your fragment on the top will capture the user clicks. Hope it was clear enough.

Example - If your fragment's layout has a Linearlayout as the parent, assign an onClickListener for it in your fragment code the same way you do for a button.

k1slay
  • 1,068
  • 1
  • 10
  • 18
  • Yes you are right. Can u tell me why it's happening is it a bug. – A J Feb 17 '15 at 19:03
  • 1
    I don't think it's a bug, the fragment registers clicks for only those elements which have an onClickListener. Otherwise the clicks are sent to the element behind the fragment. I think the reason is that a fragment only returns a view and is not a completely separate screen. – k1slay Feb 17 '15 at 19:08
  • How to apply the same with a ViewPager ? Please help me out. – Jatin Jha Jun 16 '16 at 12:02
0

You should remove previous fragment.

Try to use this code:

public static void insertFragment(Context context, int resId, Class<? extends Fragment> clazz, Bundle args, boolean stack)
    {
        if (context != null)
        {
            FragmentManager manager = ((FragmentActivity) context).getSupportFragmentManager();
            FragmentTransaction tx = manager.beginTransaction();

            if (stack)
            {
                tx.addToBackStack(null);
            }
            else
            {
                Fragment fragment = manager.findFragmentById(resId);
                if (fragment != null)
                    tx.remove(fragment);
            }
            tx.add(resId, Fragment.instantiate(context, clazz.getName(), args));
            tx.commitAllowingStateLoss();
        }
    }
anil
  • 2,083
  • 21
  • 37
  • But I need the previous fragment in same state I mean there is a dropdown as you can see based on that dropdown it going to prepare the list.So if I am going to remove the previous fragment at the time of coming back I can't get back that same state of that list. – A J Feb 17 '15 at 18:36