I have an Android Wear app that contains an activity with a timer and a view rendered accordingly. When a user swipes right on the activity to get back to the Context Stream a notification appears showing that the App is still counting.
However the user is unable to resume the timer activity from the notification intent because a brand new instance of this activity is launched. I know there is a FLAG_ACTIVITY_REORDER_TO_FRONT flag I can add to the intent but because activity is destroyed when the user swipes right on it, this does not work.
How can I properly resume the activity from the notification. I guess I somehow need to prevent it being destroyed on swiping back?
Thanks in advance!
Once you leave the app, your activities will be destroyed, so you will have to save the time you started the timer somewhere (Shared Preferences is a good option), and then read this in when your app is restarted.
Related
I am building an medication reminder app in android using java. User can set one or more reminders and according to those will get notified to take the medicine.
Problem is, whenever a notification is generated, and the user taps on it and the receiver activity opens up, the user is presented with two choices of either taking the medicine or skipping it. Now, I have made sure that in both the cases, the activity will finish and have called its onDestroy() method too. My objective is to prevent the activity from appearing in the recent app list, so that the user can not repeatedly take or skip medicines.
Here's the details(the links all point to screenshots of the app if it helps in any way):
The notification comes,User taps on the notification and the Reminder Receive activity opens up
User either takes or skips the medicine, and the activity finishes.
but the activity is still being shown on the recent app list, and if tapped on it, it opens up the reminder receive activity again, with the user able to perform the same action again
I want to prevent this particular behaviour from happening.
Here's the things I have tried(I have a fragment,running on top of the activity):
finishing the activity from fragment,then calling onDestroy() on it
ReminderRecieveActivity activity = (ReminderRecieveActivity) requireActivity();
activity.finish();
activity.onDestroy();
Modifying the onDestroy() method of the activity like so
#Override
protected void onDestroy() {
super.onDestroy();
int id= android.os.Process.myPid();
android.os.Process.killProcess(id);
System.exit(0);
}
But still the problem persists, Please help.
Why not create a separate Activity with android:noHistory="true" and also android:excludeFromRecents="true"? You don't have to kill the process to do that.
Is Service and Notification the correct way to implement a background app with the following behavior?
1- User open the app, make some configurations and touch in a "run" button;
2- The main activity must be closed and a background service will be started;
3- A fixed notification will be displayed with some buttons ("stop" to finish the service and "Reconfigure");
thank you
I did an app that at short intervals (1 to 10 sec) acquires constantly data from a remote TCP server, even on the background also with the screen off.
In the Main Activity onStop I acquire a Partial WakeLock and I start the Service with STARTFOREGROUND_ACTION intent, the Service calls startForeground and shows the Notification.
On Activity onResume the WakeLock is released, the Service stops with STOPFOREGROUND_ACTION intent and the Service itself calls stopForeground & stopSelf.
I don't have buttons in the notification, if user touches the notification the Activity is shown.
It works very well I tested it with hours of continuous operation.
i have a mediaplayer in an activity, i want to implement a feature in which when a user clicks a button, the screen display goes off, but the video continues to play in that activity.
Acording to the life cycle of the Android activity, when an activity becomes background, onPause is called, however, from now on, the system can kill the activity by its will, ie. the system requires more memory.
It'S the system that decides to kill the activity or not, not the developers. In general, It'S because more memory are needed.
Understand the Lifecycle Callbacks
You need to implement a Service. Start at this link on how to do it: https://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices
activity should be paused
you can override the procedure onPause() and onResume() to check the event
when pause, you pause the media, when resume you resume the media
the media is playing the other thread and draw the activity. when the activity is pause, but the media thread is working.
Let's say the user presses a button which causes another activity to get launched. Now the user is in another activity. What flag do I have to add to the intent, so that the user returns to my app when pressing the back button instead of navigating down the back stack of the started activity?
From the documentation, "activities on the backstack are never rearranged."
Android documentation Tasks and BackStack
You do not have to add anything to the intent. When you navigate out of you app and start another activity to complete a task, the new activity becomes kind of like an extension of your app. Pressing the back button takes you back to your app. Read This
I have an app that circles around the main activity (a main menu). In each other app there is an option menu item that directs to this activity.
At first, I always started a new main activity when this item was selected. Using the intent bundle, I did tell the main activity that some initializations I do on a fresh start were not necessary.
However, I didn't quite like the overall behavior. I stumbled upon android:launchMode="SingleTask" and this seemed to help: now I don't recreate my main menu activity all the time; also, if I press the "back" button I come back straight to the home screen. This feels quite nicely like a proper "main" menu.
My problem now is this: if I run another activity of my app, press home button and then reopen my app (e.g. using "last apps"), then I don't go back to the last activity, but to the main one. The other activity is destroyed.
Any ideas how I can implement the behavior of SingleTask without only being able to return to one activity?
If your other activities are declared normally with activity defaults in Android, then going back to your app should take you to the same activity where you left off (using the hardware home button)
However remember that the Android system kills applications when it requires system resources. So your app may have been killed when you went to the other application. Then when you get back to your app, the default launcher activity will be restarted, which is your Menu activity.
To get back to the main activity from any activity, do this:
public static void goHome(Context context) {
final Intent intent = new Intent(context, HomeActivity.class); //give name of your main activity class here
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
That will clear the activity stack and get you back to your main activity. As you declared singleTop, it will bring the existing main activity to the foreground. The flag Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all activities in the stack on top of the main activity. (I am assuming you are within the same application).
Now, all your other activities only need to include a button whose click listener invokes the method goHome();
From your main activity, if you press the hardware back button, it should exit your app.
Why not call finish() on the activities that were created by the main activity? This way you return to the main activity, without creating a new one...
I think you should save the state of you activity before starting another activity, and then resume your activity whenever you come back on last activity.
see Activity Life cycle from Android
http://developer.android.com/guide/topics/fundamentals/activities.html