startActivityForResult() and onActivityResult() in Fragments

Asked

Viewed 7,398 times

5

I have a MainActivity that allows me to open two Fragments. The class Fragment_1 has a Button that in his Listener opens a second activity through the following call:

Intent intent = new Intent(getActivity(), SegundaActivity.class);
getActivity().startActivityForResult(intent, 1);

My problem is that in class Fragment_1 I can’t capture the Intent response method onActivityResult():

if (resultCode == Activity.RESULT_OK && requestCode == 1) {
String resposta = data.getStringExtra("resposta");}

In class SegundaActivity, the reply is sent as follows::

Intent devolve = new Intent();
devolve.putExtra("resposta", "Resposta");
setResult(Activity.RESULT_OK, devolve);
finish();
  • Hello ramaral, while waiting for answers I discovered what my problem was: I could not open the Segundaactivity class through the so-called "getActivity(). startActivityForResult(Intent, 1)", ie if I add the method "getActivity()" to the method "startActivityForResult()" I won’t be able to receive the Intent response!! However I will not fail to read what you sent me. Thank you.

  • The links I sent have nothing to do with your question, which is why I deleted the comment.

  • Okay. However I send the following link that helped me to solve my problem: http://helpdev.com.br/2014/03/31/android-chamando-o-onactivityresult-fragment-onactivityresult-not-called-in-fragment/

  • Is that it? For what I’ve been reading, it’s not enough to call startActivityForResult(intent, 1) without getActivity(). In the Activity that hosts the fragment should be done the overrun of the method startActivityForResult() and call super.startActivityForResult().

  • Ramaral, with me decided... By the way, when you say override the startActivityForResult() method, didn’t you mean override the onActivityResult() method and call super.onActivityResult()? If so, I already owned it...

  • Yes you’re right: override the method onActivityResult() and call super.onActivityResult()

  • Since I was able to find the solution, you could post an answer.

  • It’s not clear what you’re asking. Please rephrase.

Show 3 more comments

2 answers

4


Example of the solution to my problem:

Class Fragment_1:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ...
    botao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), SegundaActivity.class);
            startActivityForResult(intent, 1);
        }

    });
    ...
    return viewlayout;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == getActivity().RESULT_OK && requestCode == 1) {
        String resposta = data.getStringExtra("resposta");
        Toast.makeText(getActivity(),"Mensagem Recebida da SegundaActivity:\n" + resposta, Toast.LENGTH_LONG).show();
    }
}

Segundaactivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_segunda);

    Button botao = (Button) findViewById(R.id.button1);     
    botao.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent devolve = new Intent();
        devolve.putExtra("resposta", "Resposta");
        setResult(RESULT_OK, devolve);
        finish();               
    });     
}

0

I know the topic is old but I went through the same problem and I’m using Kotlin.

In my case I had to add super. in front of the startActivityForResult In my case it is a bluetooth request, but can be used in other examples:

Fragment.kt

//Dentro da classe do Fragment
val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
private var REQUEST_ENABLE_BT = 1

//Dentro de OnCreateView
if (bluetoothAdapter?.isEnabled == false{
    val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
    super.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}

//Logo depois
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    when (requestCode){
        REQUEST_ENABLE_BT -> {
            when (resultCode) {
                Activity.RESULT_CANCELED -> {
                    //Solicitação recusada
                }
                Activity.RESULT_OK -> {
                    //Solicitação aceita
                }
            }
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.