Android:从Fragment调用Activity

Dha*_*ool 10 android android-intent android-fragments android-mediarecorder android-audiomanager

我在活动中使用片段.我正在使用MediaRecorder进行录音.我有两项活动.1st本身是将列出录制文件的Activity.在它的右侧,当一个选择记录新文件时,将调用AudioRecording Activity.当选择任何列出的文件时,我使用AudioPlayer播放录制的文件.我在这里能够将Activity转换为片段但是当我按下Stop时它终止了应用程序.

请有人可以回答.当我将它用作简单的活动时,我的audiorecorder工作正常.任何解决方案,如果我可以在该片段中调用该活动或类似的东西.?如果有人知道,请帮助我.

Chi*_*ath 33

使用get活动获取父活动,然后照常执行.

Intent myIntent = new Intent(getActivity(), BookmarkActivity.class);
getActivity().startActivity(myIntent); 
Run Code Online (Sandbox Code Playgroud)


Ish*_*era 8

这是另一种替代方法.这对我有用.

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;
        }
    }
Run Code Online (Sandbox Code Playgroud)


Sub*_*ddy 3

要调用另一个activity,请fragment使用以下命令:

Intent i = new Intent(getActivity(), Activity.class);
startActivity(i);
Run Code Online (Sandbox Code Playgroud)