Jose Almeida
Jose Almeida

Reputation: 54

Mainactivity restarts after returning from childActivity

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>
-------------------------------------

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!

Upvotes: 0

Views: 462

Answers (2)

Mario Lenci
Mario Lenci

Reputation: 10542

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()

Upvotes: 1

blahdiblah
blahdiblah

Reputation: 34031

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.

Upvotes: 1

Related Questions