dimanche 10 mai 2015

How to fix that jump to previous activity about startActivityForResult and onActivityResult?

I developed there activities which are called as Activity A, Activity B and Activity C. Firstly, Activity A is my main activity and When application is launced , Activity A is created. After that I am moving to Activity B with startActivity(this,ActivityC.class) , there is no problem up to here.The problem is When I want to moving to from Activity B to Activity C and I want to fetch datas from Activity C , however sometimes I can fetch it and sometimes I cannot fetch it and moving to Activity A.I will put my code pieces about problem , How can I be sure about that I can fetch data all the time ?

Moving from Activiy A to Activity B.

        Intent intent = new Intent(this,ActivityB.class);
        intent.putExtra(User.USER_ID,id);
        activity.startActivity(intent);

Moving from Activity B to Activity C

    Intent intent = new Intent(this,ActivityC.class);
    startActivityForResult(intent, REQUEST_CODE);

Moving from Activity C to Activity B with data.

    Intent returnIntent = new Intent(this,ActivityB.class);
    Bundle bundle = new Bundle();
    bundle.putByteArray("data",data);
    returnIntent.putExtras(bundle);
    setResult(Activity.RESULT_OK,returnIntent);
    finish();

In Activity C , I handle coming data into

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode,resultCode,data);
    if(requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK){
        try {
            Bundle bundle = data.getExtras();
            byte[] photo = bundle.getByteArray("data");
            setPhoto(photo);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

These codes works fine however sometimes after finish Activity C, application jump to directly Activity A and I want to sure that application has to pass from Activity C to Activity B.

Aucun commentaire:

Enregistrer un commentaire