Bechma Bechma - 1 year ago 57
Android Question

Why Intents are not preserved in android

I'm testing my mobile phone how many intents can be created in parallel but only one is created...

This is my app manifest

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:launchMode="standard">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".IntentExamples"
android:exported="false"
android:launchMode="standard"
android:parentActivityName=".MainActivity">
</activity>
</application>


And this is the class where I do all the calls...

public class IntentExamples extends AppCompatActivity {

// Numbers of intents created
static int COUNTER = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent_examples);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// When a intent is created +1 to the counter
COUNTER++;
((TextView) findViewById(R.id.intent_counter)).setText("NUMBER OF INTENTS: " + COUNTER);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.another_intent:
Intent intent = new Intent(IntentExamples.this, IntentExamples.class);
startActivity(intent);
case android.R.id.home:
this.finish(); break;
default:
return false;
}
return true;
}

@Override
protected void onDestroy() {
super.onDestroy();
// When a intent is completely destroy, -1 to the counter
COUNTER--;
}


The main activity just have a copy pasted code from the menu's code...

When I hit the item menu that creates another intent, in the TextView shows 1 intent created, I hit it again, shows 2 intents created and when I hit the button a 3rd time it got stuck in 2 intents created... I don't understand the reason why the counter doesn't reach 3 intents created or more.

Answer Source

Take a look at this code from onOptionsItemSelected()

switch(item.getItemId()){
    case R.id.another_intent:
        Intent intent = new Intent(IntentExamples.this, IntentExamples.class);
        startActivity(intent);
    case android.R.id.home:
        this.finish(); break;
    default:
        return false;
}

There is no break; after startActivity(), so the Activity finishes. So eventually (not necessarily immediately!) onDestroy() is called and COUNTER-- is executed.

Because the Activity has called finish(), it is not possible to reach it once more by pressing BACK - and this "misbehaviour" was what I could not explain in the first place. I was ready to believe that the Runtime would keep at most two Activity instances alive and store all the others in the BackStack. But not being able to go back? There had to be an explanation for this - and I'm kind of relieved I've found it :)

Now why does COUNTER show 1 instead of 2 after rotating the device?

That's because an orientation change (like all configuration changes) causes all Activity instances to be destroyed, and the one which was shown is instantly recreated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download