A J
A J

Reputation: 4632

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

Upvotes: 4

Views: 2658

Answers (2)

k1slay
k1slay

Reputation: 1088

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.

Upvotes: 7

anil
anil

Reputation: 2123

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

Upvotes: 0

Related Questions