Pergunta

Consider this scenario:

public class Parent extends Activity{

@Override
public void onCreate(Bundle icicle) {
     super.onCreate();
.......
}

  public void onResume(){
       super.onResume();
     ....
    onClick of BUTTON b1.....

        Intent intent = new Intent(Parent.this, Child.class);
    intent.putExtra("param", data);
     startActivityForResult(intent, ACTION_LAUNCH_CHILD_ACTIVITY);
     }

      ....
   }

  public void onActivityResult(){
    do something......
   }
 }
}

==========================================

public class Child extends Activity{

@Override
public void onCreate(Bundle icicle) {
    super.onCreate();
.......
}

  public void onPause(){
     super.onPause();
     setResult(OK);
     finsh();
     }

      ....
   }
 }
}

Steps:

  1. Launch Parent activity.
  2. Click Button b1 on Parent and launch child
  3. Child is brought successfully and shows up on screen
  4. Now hit Home Button on the Android device (hardware)
  5. Child's onPause, onStop and onDestroy is called.

But I dont see the program control entering Parent's onActivityResult when app is brought to the backgrnd.

Where is the program control for this particular when app is in background status?

On bring app back to foregrnd what is the expected behavior in terms of activity life cycle?

Thanks in advance

Can anyone explain on this please.

Foi útil?

Solução

If you remove the call to finish() in onPause, then hitting the home key calls only the child activities onPause method. When you re-launch the app, only the child activities onResume method is called. None of the parent methods seem to be called. The pattern of calling setResult in the activity and then finish in onPause FAILS after the user changes the phone orientation. So if the user enters data, saves the data (calling setResult in onSaveButtonClicked), changes phone orientation, and exits calling finish and the changes ARE NOT SAVED.

Consider calling finish() immediately after setResult at the moment you have enough data to set the result, exiting the child activity.

// UPDATE BUTTON HANDLER
    final Button buttonUpdate= (Button)findViewById(R.id.ButtonPasswordUpdate);
    buttonUpdate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {   
            String password= editTextPasswordFirst.getText().toString();
            String verify= editTextPasswordSecond.getText().toString();  
            String err="";

            if (password.equals(verify) && (password.length() >= minimumPasswordLength)) { // 1) SUCCESS
                ResetTimeoutValues(timeoutType); // new password so calculate new timeout time     
                isValidKey= true;
                PasswordState outPasswordState= new PasswordState(lengthKey,
                        timeExpire,
                        isValidKey,
                        timeoutType,
                        password,
                        isHashPassword,
                        minimumPasswordLength);
                Bundle b= new Bundle();
                b.putSerializable("jalcomputing.confusetext.PasswordState", outPasswordState);
                getIntent().putExtras(b);
                setResult(RESULT_OK,getIntent());   // call home with data on success only
                finish(); // go back <=== EXITS Here
                return;
            }...
    });
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top