0

I want to transact from one fragment to another... below given is my snippet...

I want transaction on button click...

    public class Main extends Fragment {
 // final View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

     View rootView = inflater.inflate(R.layout.main, container, false);
     Button camera=(Button)rootView.findViewById(R.id.button1);
     camera.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.layout.main, new CameraActivity());
        ft.commit();
        }
     });

    return rootView;

    }

so please Assist me in solving this...

[1]<--->[2]<--->[3]<--->[4]<--->[5]

where []=fragments...

[1]- has buttons a,b,c,d,e
How to move from 1 to 3 OnClick of c ...

Here is the Camera Activity

 public class CameraActivity  extends Fragment {

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

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

    return rootView;
}
    }
Banku
  • 287
  • 3
  • 24

1 Answers1

0

first of all this cant be activity

 ft.replace(R.layout.main, new CameraActivity());

it has to be fragment put something like this in your onclick listener in FirstFragment:

Bundle bundle = new Bundle();
bundle.putString("key", value); //if you want to pass parameter to second fragment
SecondFragment fragment = new SecondFragment();
fragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment).addToBackStack(null);
ft.commit();

than in your SecondFragment onclick listener:

Bundle bundle = new Bundle();
bundle.putString("key", value); //if you want to pass parameter to second fragment
ThirdFragment fragment = new ThirdFragment();
fragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment).addToBackStack(null);
ft.commit();

and so on ...

method addToBackStack(null) is providing you back as simple as that. You could also pass some tag in that method if you need it, but I think from question you want be needing it.

Also if you need to start Activity than dont use this method, use Intent instead.

hope it helps

Darko Rodic
  • 995
  • 3
  • 10
  • 27
  • CameraActivity is a class file not Activity ... and i want to simply go to other class (Like Intent in Activity) – Banku Feb 11 '14 at 13:00
  • first of all, if CameraActivity extends Fragment than please call it CameraFragment, it is a lot more natural... also, answer I provide you works :) – Darko Rodic Feb 11 '14 at 13:09