androidandroid-fragmentsandroid-listfragment

Previous fragment element is still active after adding new fragment


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


Solution

  • 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.