Android: Minimizing and resuming a Activity - android

I am pretty new to Android development .
My problem is a bit tricky one.
I want to develop an application using 2 activities.
1st activity has a button . On clicking the button I want activity 2 to get started as follows:
Activity 2 will come to foreground for 2 seconds and then goes to background for 8 seconds , after which it will again come to foreground for 2 seconds and then again goes to background and the process continues.
Meanwhile both activities should continue their respective tasks.
For ex. We can have a Activity such as Music player which is playing music and another activity named Activity 2 which is downloading some files.
I have tried many things ranging from using Intents to minimizing a activity and displaying notification on notification bar. The problem with notification bar is that it on resuming the activity using notification bar it is always calling OnCreate() method thus again starting the activity.
I am able to start a activity on button click but don't know how to minimize it and then pop it up in same state .
I am using services in background for timing delays .
Please Help and share your solutions to this problem.

I have solved the problem.
The solution is to modify manifest file as :
add to main activity code
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
add to child activity code
android:launchMode="singleInstance"
android:clearTaskOnLaunch="true"
Its works perfectly for my app :)
thank you all for your kind contribution

Maybe u found solution for this issue as u said, but in any case, I would recommend reading some things.. It will help you to better understand that:
There is no such a thing as minimizing activity (Activity Lifecycle),
Activity/Intent Flags and lunching modes
Tasks and back stack
What is service used for,
What are AsyncTasks,
How to use Handler
Oki.. I made some effort and organize links for you. I know it seems a lot of material, but you don't have to run to read them in next 15min.. Take it easy, read, try, learn. Take this as advice, cause once u get used to do things in a wrong way it will be much more difficult to correct them later..
Hope you find it useful.. ;) Cheers

moveTaskToBack(true); moves the whole task to back i.e. it moves both activities to back . Then the problem will be to move the activity to foreground which I don't know.

Have you tried using
this.moveTaskToBack(true);
I'm not sure if this is what your looking for or not. Heres the Documentation as well!

Related

Detail Explanation of LaunchMode in Android

I want to learn the concept and idea for using launchMode in Android. When to use which mode and a little detail about each of them.
Because I am facing an issue in Animation Transition between Activity. As i go from Activity A to B. Amimation shows smoothly but when i go back to Activity A by calling finish() Animation doesn't look as it goes from B to A. I googled it and some answer says to add singleInstance or singleTask to Activity in manifest. And some say to add flag Activity_reorder_to_front and so many other answer. Now I don't want anyone to solve my issue. I just want to learn these concept to get my job done myself.
and also suggest me which one is preferable for my current situation. I must call finish and i don't want to create a new Instance of Activity A.

Start an activity from any other app

I'm designing an app composed two activities. The first one always run, and is asked to trigger a second one when some stuff happens. This works fine with the standard code used for running activities:
Intent myIntent = new Intent(this, allarme.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
however, the activity in allarme.class is not started if i'm using another app (i.e. gmail),
whilel it works perfectly from home or when the screen is locked.
I'm sure that the first activity is still running, it's just that the second action is not triggered.
Should I change anything in the manifest file to fix this?
I'm not really clear about what you want.
but I guess you want to run two activities simultaneously, am I right?
while one activity run in background and one activity update the UI.
always keep in mind that Android architecture only allowed one activity to run at the time. If you want to pass the data from Asynchronous Task, you can call the method from that class and let your handler 'recent' class update the UI.
You can create a background service that always run at background even if you are using another app or phone is locked. Here is link for more help :
http://developer.android.com/training/run-background-service/create-service.html
Thanks everybody, I think I solved it.
Basically, I found out that an activity already running can be brought to focus when re-started with a basic startActivity. Then I can immediatley switch to the new activity without losing the focus.

Saving State With Android

I have an app that will never require more than one instance of an activity. I want it so that when the user comes back to a screen it is in the same state as they left it except for a few places where it doesn't make sense. I've worked out saving the persisted data with onpause onstop updates. However to keep the screen looking the way it did when they left it i use intents specifically setting the flags to Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_SINGLE_TOP then startActivity. It seems to work great but does it make sense? Is there a smarter way? Pitfalls doing it this way etc... any feedback will be greatly appreciated.
android:launchMode = "singleTask"
add the above line for every activity in the manifeast file. Adding these launch relaunch the activity instead of creating the activity again.
Refer this link

Android: Keep MediaPlayer running during Activity screen orientation update

I use a MediaPlayer to play an MP3. Currently I disabled screen orientation changes by using
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation"
in the manifest. I do want to support landscape mode now - i.e. removed those tags - but have the problem that during the destroy/create cycle the player gets stopped and then restarted. This is okay and I actually do this even manually in onPause() to stop the player when the activity goes in the background.
To keep it running during orientation changes now, I tried making it static (and using the Application Context to create it once). Of course, when I remove the player.stop() in onPause() now, it does what I want - well, until the Activity goes in the background.
So, two questions:
How can I determine if the Activity will be recreated directly after the call to onStop()
Or: How can I keep the MediaPlayer running during that cycle, yet stop it when the App goes in the background?
Have you looked at using the onConfigurationChanged() callback to handle some of this logic?
Regarding your question how you can reliably determine whether your Activity is destroyed due to a configuration change, see my answer here: How to save/restore(update) ref to the dialog during screen rotation?(I need ref in onCreate method of activity.)
With this, the answer to your second question should be easy.
Try running MediaPlayer in different Thread.
You can add to this thread a more complex API to which you can call from onCreate/onStop/on*
If you are using fragments (and you should :P), then calling setRetainInstance(true) inside your fragments onCreate() call will make this problem go completely go away.
Please see the following answer: https://stackoverflow.com/a/31466602/994021
It is a POC I've created for this purpose. Tired of solving this issue over and over again for different projects. I hope it helps.
I really recommend to switch to a third party player like ExoPlayer from google guys: https://github.com/google/ExoPlayer

activities stacking up problem

this might be an easy question but i have a notification that when clicked open up the stock messaging app
arg1.setClassName("com.android.mms","com.android.mms.ui.ConversationList");
but i noticed that every time it is clicked the activity keeps stacking up and i end up having to use the back button a bunch of times to get out of it. I have always used android:noHistory="true" in the manifest but obviously i cant do that here so is there a way to do the same thing with when the intent is launched?
Try using arg1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) before calling your intent, which might solve your problem...
You can use Intent.FLAG_ACTIVITY_REORDER_TO_FRONT. If the activity is already running, it'll be brought to the front.

Categories

Resources