1

I have three Activities. first and second Activities has a button "doProcess". Third Activity execute some process. I want to do next scenaries (in pseudocode):

Scenary 1:

1) in FirstActivity click button "doProcess";
2) run ThirdActivity, put FirstActivity in backstack;
3)
if operation finish successful
    destroy FirstActivity;
    run SecondActivity and put here result;
    destroy ThirdActivity;
else
    back to FirstActivity;
    destroy ThirdActivity;

Scenary 2:

1) in SecondActivity click button "doProcess";
2) run ThirdActivity, put SecondActivity in backstack;
3)
if operation finish successful
    back to SecondActivity and put here result;
    destroy ThirdActivity;
else
    back to SecondActivity;
    destroy ThirdActivity;

Could you explain me what methods i need to call from each Activity for running other activity which i need. I understand that for Scenary 2 I may run startActivityForResult() from SecondActivity and override onActivityResult(). And in ThirdActivity I call the setResult() method. But i'm not understand how to do the Scenary 1.

1 Answer 1

0

The onActivityResult(...) invokes by system before onResume() that's why we can redirect intent from thirdActivity to secondActivity.

Here is the code. In onCreateView() we start second activity with startActivityForResult(). When the result is returned by secondActivity, will be running an onActivityResult(). In this method we take result from intent, called data, and then put it into a new intent. Then we start secondActivity with this intent by invoking startActivity().

import android.app.*;
import android.os.*;
import android.view.*;
import android.content.*;

public class FirstFragment extends Fragment {

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

    // intent redirecting
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(data == null) {
            return;
        }
        Intent intent = new Intent(mContext, SecondActivity.class);
        ...
        intent.putExtra(...);
        startActivity(intent);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.