0

Suppose there are only 2 Activities in an Application :
1. Activity A (Launcher Activity)
2. Activity B


Code for Acrivity A in onCreate() :

Intent intent = new Intent();
    intent.putExtra("key", "test");
    intent.setClass(this, ActivityB.class);
    startActivity(intent);
    finish();  

So, launching Activity B from Activity A, by passing a data. Also Activity A get destroyed.


So, if I launch the app for first time :
1. Activity A started
2. Activity A launches Activity B with a data
3. Activity A get destroyed

Suppose If I press the back key from Activity B, Activity B get destroyed and the application get exit and if I re-launch the app :
1. Activity B get started directly, getting the same data, that was set from Activity A.


My Question is :
How Can I stop getting this intent when app get re-launched ?
Activity B started after re-launch, is not an issue, I just wanted to stop getting the intent.


AndriodManifest.xml :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.listnertest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="ActivityA"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="ActivityB"
        android:label="@string/app_name" >
    </activity>
</application>

Shanki Bansal
  • 1,681
  • 2
  • 22
  • 32
  • What Intent flags are you using? Anything in your AndroidManifest relating to that? – shkschneider Dec 03 '14 at 14:13
  • When you restart the app, Activity A should get launched. Same as the first time. – David Wasser Dec 03 '14 at 14:37
  • I am not using any flag. Also relaunching the application ActivityB gets start-up. Re-launch can be done from 2 places. first from clicking the launcher icon, and second from the Recent Apps. – Shanki Bansal Dec 03 '14 at 17:17

1 Answers1

0

Every time you launch the app, it's going to run ActivityA. And since you are telling ActivityA to send that data to ActivityB whenever it is created, it's going to do that every time.

It sounds like on the second time, you still want to launch ActivityB, but not with the data you put in the intent extra, is that right? You would need to keep track across app launches whether you have sent that data yet. A convenient way to do that is SharedPreferences.

Intent intent = new Intent();
SharedPreferences prefs = activity.getSharedPreferences("my_prefs", 0);
if (!prefs.contains("sent_key")) {
    intent.putExtra("key", "test");
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("sent_key", true);
    editor.commit();
}
intent.setClass(this, ActivityB.class);
startActivity(intent);
finish();  

This will make ActivityA always launch ActivityB, but it would only send the data the first time.

Bruce
  • 2,377
  • 1
  • 17
  • 11
  • Actually, if I re-launch the app, ActivityB get launched without launching of ActivityA and launching of ActivityB is not in my control, it is called automatically by the android system. Question is not much clear to you, but from your solution, I get an idea of shared preference. When ActivityA launch, I put the data into shared preference, and when ActivityB launch I get that data from shared preference and after that clear that shared preference, so second time, I will not get the data. Thanks for reply. :) – Shanki Bansal Dec 04 '14 at 10:08