Reputation: 1298
I started an activity with dialog theme from onReceive() method by using context variable. The started activity is having alert dialog layout with Ok and Cancel buttons. When user taps on Ok/Cancel button I am calling finish() method so the activity getting destroyed. But when I launch the application this activity is coming again. Is this because of starting the activity with context variable. Even if I set FLAG_ACTIVITY_NEW_TASK also it is coming again and again when i launch the application. Can someone please help me how can I avoid this activity.
EDIT
public class C2DMMessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("com.google.android.c2dm.intent.RECEIVE".equals(intent.getAction())) {
Intent customDialogIntent = new Intent(context,DialogActivity.class);
customDialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(customDialogIntent);
}
}
}
public class DialogActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_dialog);
TextView textAlertTitle = (TextView)findViewById(R.id.textAlertTitle);
TextView textAlertMessage = (TextView)findViewById(R.id.textAlertMessage);
Button button1Alert = (Button)findViewById(R.id.button1Alert);
Button button2Alert = (Button)findViewById(R.id.button2Alert);
textAlertTitle.setText("Notification");
textAlertMessage.setText("One notification is waiting for you. Do you want to see the notification?");
button1AlertOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(DialogActivity.this,HomeActivity.class)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity();
}
});
button2AlertCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.ww.activity" android:versionCode="1" android:versionName="1.5">
<uses-sdk android:minSdkVersion="8" />
<permission android:name="com.sample.ww.activity.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.sample.ww.activity.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="SplashActivity" android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER"></category>
<action android:name="android.intent.action.MAIN"></action>
</intent-filter>
</activity>
<activity android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait" android:name="HomeActivity"></activity>
<activity android:theme="@style/AlertDialog.NoTitleBar"
android:screenOrientation="portrait"
android:name="DialogActivity"
android:launchMode="singleInstance"
android:taskAffinity="com.sample.ww.activity">
</activity>
<receiver android:name="com.nielsen.ww.receiver.C2DMMessageReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.sample.ww.activity" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.sample.ww.activity" />
</intent-filter>
</receiver>
Upvotes: 0
Views: 1860
Reputation: 95578
Don't use launchMode="singleInstance"
. You don't need that.
When the user taps "OK" on your DialogActivity, you start the HomeActivity but you do not finish DialogActivity. That's why, when you return to the application, you still see the DialogActivity. You need to call finish()
after you call startActivity()
in your onClick()
method.
taskAffinity
set to "com.sample.ww.activity" for all of your activities (even if you don't explicitly specify it, it defaults to the name of your package which is "com.sample.ww.activity"). If you want to have the activities run in different tasks then they need to have different taskAffinities. In any case, you may not need to have the activities running in different tasks if you fix the finish()
problem in #2.Upvotes: 1