My question is that I have two activities, Activity A and Activity B. Activity A is the main activity, Activity B is only accessible by touching a notification.
I need to be able to touch a notification, which will open Activity B. After the user is finished with Activity B, they can press the back button or finish Activity B. When Activity B closes, Activity A should be shown. Currently, I close Activity A (which closes the entire app) and receive a push notification. I select the push notification which opens Activity B. When I close Activity B, Activity A is not shown and the entire app closes. I want Activity A to be there after finishing Activity B.
I am using Parse SDK notifications. In my custom ParsePushBroadcastReceiver I have the following code
public class MyReceiver extends ParsePushBroadcastReceiver
{
@Override
public void onPushOpen(Context context, Intent intent)
{
Intent i = new Intent(context, NotificationActivity.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
I have it listed in the Android Manifest. My notification activity in manifest is
<activity
android:name=".NotificationActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="portrait"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
In my notification activity I have the following code in onBackPressed
public class NotificationActivity extends ActionBarActivity
{...
@Override
public void onBackPressed()
{
if(NavUtils.getParentActivityName(this) != null)
{
Log.d(Constants.NAVIGATION_DEBUG, "Get Parent Activity NOT NULL");
NavUtils.navigateUpFromSameTask(this);
}
else
{
Log.d(Constants.NAVIGATION_DEBUG, "Get Parent Activity NULL");
super.onBackPressed();
}
}
...}
Surprisely, in the logs it states that it is NOT NULL, however it doesn't navigate back to Activity A if the app was closed prior to pressing the notification. Instead the app closes. I want Activity A to show whether the app was closed when the notification was pressed or whether the app is open. THANKS!
You can try the following method: put some boolean extra in your intent, and then start MainActivity. That extra will be the indicator of NotificationActivity start.
public class MyReceiver extends ParsePushBroadcastReceiver
{
@Override
public void onPushOpen(Context context, Intent intent)
{
Intent i = new Intent(context, MainActivity.class);
i.putExtras(intent.getExtras());
i.putExtra("IS_FROM_NOTIFICATION", true);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Then in your MainActivity's
onCreate()
method, do something like this:
public class MainActivity extends ActionBarActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getIntent().getBooleanExtra("IS_FROM_NOTIFICATION", false)) {
startActivity(MainActivity.this, NotificationActivity.class);
}
}
// other initialization code
...
...
}
Then you don't need to override onBackPressed
in your NotificationActivity
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With