Basically I'm calling a new activity (ExplorerActivity) from parent (MainActivity) Demonstration with a mix of pseudo code...

public class MainActivity extends Activity {

boolean isLoggedin=false;

onCreate(){
 Print(isLoggedin)
 isLoggedin=true;
}


public boolean onOptionsItemSelected(MenuItem item) {

Intent i = new Intent(MainActivity.this, ExplorerActivity.class);
 startActivityForResult(i, 0);
 return true;
}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
     super.onActivityResult(requestCode, resultCode, data);
     Log.e("Teste", "Mainactivty: onActivityResult was called!!");
     xTaskThread = new xTaskThread();
     xTaskThread.start();
}

------------ ExplorerActivity -----
public class ExplorerActivity extends ListActivity {
Intent i = getIntent();
i.putExtra("fileURL", file.getAbsolutePath());
setResult(RESULT_OK, i);
finish();

Manifest:
<activity
        android:name="com.geoclient.misc.ExplorerActivity"
        android:label="@string/app_name" 
        android:screenOrientation="landscape">
</activity>
-------------------------------------
  • Everything works fine, until returns from ChildActivity (ExplorerActivity). Follow order succeed:

    1. ExplorerActivity, finish activity... (finish())
    2. OnCreated called, printed('isLoggedin=FALSE') <--- (I expected TRUE) Shouldn't keep state on variable?? (Same as restart of application...)
    3. onActivityResult called, printed('Mainactivty: onActivityResult was called!!')

It just look like the app was restarted... I don't understand.. I was expecting to get back to onResume() and variables were in same state.. Please let me know where I'm wrong! (I'm working with googlemap in Mainactivity, not sure if this is relevant..) Many thanks!

有帮助吗?

解决方案

You haven't fully understood the Activity lifecycle. An activity can be killed at any time it's not in the foreground, or when something changes (like orientation).

If you have data, like instance variables, that you want to save the state of, do so in onSaveInstanceState. That's what it's for.

其他提示

Looking at the manifest lines you wrote there, ExplorerActivity seems to be forced to landscape orientation.

so when you go in there every Activity in the back stack will lose the state if not saved/restored in onSaveInstanceState() - onRestoreInstanceState()

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top