Android : Calling Activity from Fragment

前端 未结 7 1269
我寻月下人不归
我寻月下人不归 2020-12-02 21:43

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 record

相关标签:
7条回答
  • 2020-12-02 21:58

    In Fragment Class

     getActivity().startActivity(new Intent(gwtActivity(),MainActivity.class));
     getActivity().finish();
    
    0 讨论(0)
  • 2020-12-02 22:00

    You can simply call

    startActivity(new Intent(getActivity(),TheNextActivity.class));
    
    0 讨论(0)
  • 2020-12-02 22:05

    Your fragment should have a parent

    Intent intent = new Intent(getActivity(), SecondActivity.class);
    getActivity().startActivity(intent);  
    
    0 讨论(0)
  • 2020-12-02 22:10

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

    Intent myIntent = new Intent(getActivity(), BookmarkActivity.class);
    getActivity().startActivity(myIntent); 
    
    0 讨论(0)
  • 2020-12-02 22: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;
            }
        }
    
    0 讨论(0)
  • 2020-12-02 22:13

    To call another activity from fragment use this:

    Intent i = new Intent(getActivity(), Activity.class);
    startActivity(i);
    
    0 讨论(0)
提交回复
热议问题