14

I am using fragments inside an activity. I am using MediaRecorder to for audio recording. I have two part of an activity. 1st itself the Activity which will list the recorded file. On it's right side the AudioRecording Activity is called when one select to record for a new file. When the any of the listed file is selected i am using AudioPlayer as to play the recorded file. I am here able to convert the Activity into fragment but when i press on Stop it is terminating the application.

Please anyone can answer. My audiorecorder is working fine when i use it as simple activity. Any solution like if i can call that activity in that fragment or something like that.? Please help me if anyone knows.

Rasel
  • 15,499
  • 6
  • 40
  • 50
Dharma Cool
  • 197
  • 1
  • 2
  • 11

7 Answers7

35

Get the parent activity using get activity then do as usual.

Intent myIntent = new Intent(getActivity().getapplicationcontext(), BookmarkActivity.class);
getActivity().startActivity(myIntent); 
Community
  • 1
  • 1
Chinmoy Debnath
  • 2,814
  • 16
  • 20
10

Here is another alternative method. This worked for me.

public class **YourFragmentClass** extends Fragment {

    Context context; //Declare the variable context

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

    //Pass your layout xml to the inflater and assign it to rootView.
      View rootView = inflater.inflate(R.layout.**yourfragmentxml**, container, false); 
            context = rootView.getContext(); // Assign your rootView to context

            Button **yourButton** = (Button) rootView.findViewById(R.id.**your_button_id**);
            **yourButton**.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Pass the context and the Activity class you need to open from the Fragment Class, to the Intent
                    Intent intent = new Intent(context, **YourActivityClass**.class); 
                    startActivity(intent);
                }
            });
            return rootView;
        }
    }
Ishara Amarasekera
  • 1,387
  • 1
  • 20
  • 34
3

To call another activity from fragment use this:

Intent i = new Intent(getActivity(), Activity.class);
startActivity(i);
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
SubbaReddy PolamReddy
  • 2,083
  • 2
  • 17
  • 23
2

You can simply call

startActivity(new Intent(getActivity(),TheNextActivity.class));
1

In Fragment Class

 getActivity().startActivity(new Intent(gwtActivity(),MainActivity.class));
 getActivity().finish();
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Divyesh Murani
  • 361
  • 4
  • 8
0

Your fragment should have a parent

Intent intent = new Intent(getActivity(), SecondActivity.class);
getActivity().startActivity(intent);  
wogsland
  • 9,106
  • 19
  • 57
  • 93
Ashok Reddy M
  • 330
  • 3
  • 9
0

Best way of calling Activity from Fragment class is that make interface in Fragment and add onItemClick() method in that interface. Now implement it to your first activity and call second activity from there.

Krishna Meena
  • 5,693
  • 5
  • 32
  • 44