Start a new Activity from Fragment

前端 未结 7 1987
遇见更好的自我
遇见更好的自我 2020-12-05 08:09

Using Android Studio, I have my MainActiviy class with a Placeholder fragment. This fragment has buttons, but one has to load an Activity. How does one do this? I was tol

相关标签:
7条回答
  • 2020-12-05 08:23

    For fragment you have to use getactivity().... For Example:

    Intent intent = new Intent(getActivity(), AnotherActivity.class);
    startActivity(intent);
    
    0 讨论(0)
  • 2020-12-05 08:28

    If it is in holder:

    holder.itemView.setOnClickListener {
           val intent = Intent(context, AnotherActivity::class.java)
           intent.putExtra("text", "From fragment") . // send data
           context.startActivity(intent)
    }
    

    And in the AnotherActivity:

    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import kotlinx.android.synthetic.main.activity_another.*
    
    
    class AnotherActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_another)
        }
    
        public override fun onStart() {
            super.onStart()
            textView.text = "Received text:" + intent.getStringExtra("text")!!
        }
    }
    
    0 讨论(0)
  • 2020-12-05 08:33

    If you have a look at the documentation you can see that to start an activity you'll want to use the following code

    Intent intent = new Intent(getActivity(), AnotherActivity.class);
    startActivity(intent);
    

    Currently you're using MainActivity.class in a place that requires a context object. If you're currently in an activity, just passing this is enough. A fragment can get the activity via the getActivity() function.

    Your full code above should look like this

    Button button = (Button) rootView.findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), AnotherActivity.class);
            startActivity(intent);
        }
    });
    
    0 讨论(0)
  • 2020-12-05 08:38

    In case of fragment, write getActivity() to get activity context instead of giving the name of activity explicitly. For example, if you want to open SecondActivity from a fragment,

    Intent intent=new Intent(getActivity(),SecondActivity.xml); StartActivity(i);

    0 讨论(0)
  • 2020-12-05 08:43

    If you have to use it inside onBindViewHolder, you may do this:

    @Override
    public void onClick(View view) {
        Intent intent= new Intent(view.getContext(), MainActivity.class);
        view.getContext().startActivity(intent);
    }
    
    0 讨论(0)
  • 2020-12-05 08:43
    Button button = (Button) view.findViewById(R.id.button_ID);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent myintent = new Intent(getActivity(), CallingActivity.class);
            startActivity(myintent);
        }
    });
    
    0 讨论(0)
提交回复
热议问题