Android Intent - Activity not Resumed - android

I'm having trouble resuming an activity.
Here's what happening:
Activity A calls activity B :
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
Then Activity B Sets a Notification and calls Activity A:
Intent intent = new Intent(this, MrpCastPlayerActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, 0);
android.support.v4.app.NotificationCompat.Builder notif = new NotificationCompat.Builder(this);
notif.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("TITLE")
.setContentText("CONTENT")
.setAutoCancel(true);
notif.setOngoing(true);
Notification notification = notif.build();
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationmanager.notify(0, notification);
Intent intent2 = new Intent(B.this, A.class);
startActivity(intent2);
The problem is when I tap the notification it opens Activity B but it does't call the onResume it calls onCreate.
If I use moveTaskToBack(true), instead of starting intent2, onResume is called.
It seems that Activity B is finished when I use intent2 to open Activity A.
Is there a away to avoid this ?

To resume your activity, you should considering adding flags to your Intent.
do nothing inside the method onResume() and onstart() when coming back to this activity, Try this
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Manifest Changes:
<activity android:name=".MyActivity"
android:configChanges="keyboardHidden|orientation">
Hope this helps

Related

Return to main actuality after open activity B from notification

code create notification from BroadcastReceiver
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(G.context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(G.context.getString(R.string.app_name))
.setContentText("text")
.setAutoCancel(true)
.setColor(Color.MAGENTA);
Intent intent = new Intent(G.context, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(G.context);
taskStackBuilder.addParentStack(MainActivity.class);
taskStackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) G.context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2, mBuilder.build());
If you are currently in MainActivity.java and then click on notification and go to B Activity the just simply call this.finish() or onBackPressed() in your back press control onClick() method. If your are opening your B Activity from any where by clicking one notification then Call below method in your B activity.
#Override
public void onBackPressed() {
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
Other way you can do like this
You Dont need do anything. By default when you move from Activity A to Activity B, Android adds the Activity A to the backstack. When you press the back button from Activity B or finish it, it automatically restore the Activity A from backstack.
If you wish to finish your Activity B programmatically call activity's finish() method when you want to do it.
For more detail Check this

interrupting activity and multiple instances of activity issues

My problems are hopefully related to each other. I run a countdown timer service that fires an intent in the onfinish method. When on finish completes it automatically opens/ bring the app up which I don't want it to do. why is this so? id like it to just show the notification. Also when I click on the notification instead of just resuming the activity it creates another one. I figured the problems were related to each other due to the intents. here is the code
from onFinish() not from the notification
Intent intent = new Intent(BroadcastService.this, MainActivity.class);
intent.putExtra("id1",id1);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
and here is the code for the notification
NotificationCompat.Builder notificBuilder = new NotificationCompat.Builder(this);
notificBuilder.setContentTitle("Loot");
notificBuilder.setContentText("Claim your Gold now!");
notificBuilder.setSmallIcon(R.drawable.gold2);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificBuilder.setSound(soundUri);
notificBuilder.setAutoCancel(true);
Intent backhome = new Intent(this, MainActivity.class);
backhome.putExtra("id2",id2);
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
taskStackBuilder.addParentStack(MainActivity.class);
taskStackBuilder.addNextIntent(backhome);
PendingIntent pd = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notificBuilder.setContentIntent(pd);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notifID, notificBuilder.build());
isNoteActive = true;
}
If you don't want to open your MainActivity, then you shouldn't write this code:
Intent intent = new Intent(BroadcastService.this, MainActivity.class);
...
startActivity(intent);
Opening MainActivity is exactly what this does.

Activity gets minimized when launching it from foreground service

I have two Activities in my app:
A - login Activity
B - main Activity
When the user clicks the login button in activity A I am starting a service. In the service's onCreate() method I launch activity B like this:
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat
.Builder(getApplicationContext());
builder.setContentIntent(pendingIntent);
builder.setContentTitle("Activity B in foreground service");
builder.setSmallIcon(R.drawable.ic_launcher);
startForeground(1, builder.build());
The problem is that Activity B launches minimized. I have to press the service button in the notification bar in order to get my activity maximized (fill the screen). How could I launch Activity B from service in normal way - without minimizing it (putting to background)?
Your code doesn't actually launch Activity B at all. All it does is create a Notification, that, if selected by the user, will launch Activity B.
If you want to actually launch Activity B, do this:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Now launch the activity immediately
startActivity(notificationIntent);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat
.Builder(getApplicationContext());
... (rest of your code here)
Question already answered in android start activity from service
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
http://developer.android.com/training/basics/firstapp/starting-activity.html
Finally I found out the solution:
onCreate() method in service class:
#Override
public void onCreate() {
Intent notificationIntent = new Intent(getBaseContext(), MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
getApplication().startActivity(notificationIntent);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat
.Builder(getApplicationContext());
builder.setContentIntent(pendingIntent);
builder.setContentTitle("Activity B runs in a foreground service");
builder.setSmallIcon(R.drawable.ic_launcher);
startForeground(1, builder.build());
}
In AndroidManifest.xml add this line in Activity element:
android:launchMode="singleTop"

Clicking on Big View Notification doesn't launch activity

Clicking on the notification doesn't launch any activity. I followed the developers code but something isn't working. Any suggestions?
Intent dismissIntent = new Intent(getActivity(), Activity1.class);
dismissIntent.setAction("action1");
PendingIntent piDismiss = PendingIntent.getService(getActivity(), 0, dismissIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Intent notifyIntent = new Intent(getActivity(), Activity2.class);
notifyIntent.setAction("action2");
PendingIntent piNotify = PendingIntent.getService(getActivity(), 0, notifyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getActivity())
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("Alert")
.setContentText("Alert text")
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Big text message"))
.addAction(R.drawable.icon1,
"Dismiss", piDismiss)
.addAction(R.drawable.icon2,
"Notify", piNotify);
NotificationManager manager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
I also added android:exported="true" to the activity in the Manifest
I think the problem is that you are calling
PendingIntent.getService()
instead of
PendingIntent.getActivity()
as the doc say:
PendingIntent.getService :
Retrieve a PendingIntent that will start a service, like calling Context.startService(). The start arguments given to the service will come from the extras of the Intent.
PendingIntent getActivity : Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent). Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.

How do you build an Android back stack when an activity is started directly from a notification?

I have two activities:
Activity A - list of items
Activity B - detail view of an item
Normally, a user opens the app and Activity A is launched. A user sees a list of items, clicks one, and Activity B is started to display the item detail.
Activity B can also be started directly from clicking on a notification. In this case there is no back stack.
How can I make it so that when Activity B is started directly from a notification, the user can click the Back button and go to Activity A?
You can add an Extra into the Intent launched by the notification to detect when the app has been launched in that way.
Then you can override the onBackPressed() Activity method and handle that scenario, e.g.
#Override
public void onBackPressed()
{
Bundle extras = getIntent().getExtras();
boolean launchedFromNotif = false;
if (extras.containsKey("EXTRA_LAUNCHED_BY_NOTIFICATION"))
{
launchedFromNotif = extras.getBoolean("EXTRA_LAUNCHED_BY_NOTIFICATION");
}
if (launchedFromNotif)
{
// Launched from notification, handle as special case
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
mActivity.startActivity(intent);
finish();
}
else
{
super.onBackPressed();
}
}
You should take care of this when you receive the Notification.
I have a similar situation solved:
Intent intent = new Intent(context,ListDetail.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(ListDetail.class);
stackBuilder.addNextIntent(intent);
PendingIntent contentIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder mBuilder = new Notification.Builder(context);
mNotifM.notify(NotificationId.getID(), mBuilder.setStyle(new Notification.BigTextStyle(mBuilder)
.bigText(bigText)
.setBigContentTitle(title)
.setSummaryText(summaryText))
.setContentTitle(title)
.setSmallIcon(icon)
.setContentText(summaryText)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setTicker(bigText)
.build());
You need to set in your Manifest the hierarchy of the Activities:
<activity
android:name=".ListDetail"
android:label="Detail"
android:parentActivityName=".List" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".List" />
</activity>
I have tried one sample.Please go through with this link
https://github.com/rajajawahar/NotificationBackStack
Activity you want to launch..
Intent launchIntent = new Intent(context, SecondActivity.class).putExtra("Id", id);
Parent Activity, if back pressed
Intent parentIntent = new Intent(context, FirstActivity.class);
Add Both the activity in the taskbuilder
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
PendingIntent resultPendingIntent = stackBuilder.addNextIntentWithParentStack(parentIntent).addNextIntent(launchIntent).getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder notificationCompatBuilder =
new NotificationCompat.Builder(context);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, notificationCompatBuilder.build());
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationCompatBuilder.setAutoCancel(true).
setContentTitle("First Notification").
setContentText("Sample Text").
setSmallIcon(R.mipmap.ic_launcher).
setContentIntent(resultPendingIntent);
mNotifyMgr.notify(id, notificationCompatBuilder.build());
catch the back-button-key event with onKeyDown()-method and let the user go to activity A. Don't forget to return true to prevent the event from being propagated further.
http://developer.android.com/reference/android/app/Activity.html#onKeyDown(int, android.view.KeyEvent)

Categories

Resources