Reusing the main activity - android

I have a main activity from where I will be switching from one activity to
another..So i don't want to re initialize every time..After the first time
of creation , the same activity should be called without having to create it
over and over again..how do I do this? #newbie-android

What you want to do is simply start both your activities and switch between them by bringing them to the foreground. You should refer to this question for a possible solution. This documentation might also help.

Related

Is it possible to edit the content of the parent Activity when using SlidingActivity

I'm creating an app and I'm using a library called "SlidingActivity".
[Github Link]
I actually have two activities. One being the main activity of the app and the other one extending SlidingActivity. So when the SlidingActivity is opened, it's still possible to see the main activity in the background (see the images on the Github page).
Is it possible to edit the content/layout of the main activity when the SlidingActivity is opened?
I tried using getParent() but it's returning null.
Edit: As #Hamza Hathoute suggested I've tried overriding onPause() and onDestroy(). I've seen that onPause() is called each time the SlidingActivity is opened.
Thanks in advance. I'm new to StackOverflow so if there is anything I've done wrong please tell me!
The issue you are facing is one of communication. That is, you want the SlidingActivity to tell the MainActivity that it should change its content. While there are a few approaches to this issue the simplest might be to use the LocalBroadcastManager to send a broadcast.
Edit:
An activity that is not in the foreground can be killed by the OS in low memory situations. So you should register your receiver in onCreate and unregister in onDestroy. It is therefore possible that you might miss a broadcast (if your activity was destroyed when the broadcast was sent).
If you want to cover this case then unless you want to deal with persistence (shared prefs, db) then you should probably use the startActivityForResult option mentioned in another answer. The downside of that approach is that the changes to MainActivity aren't immediate. So if the sliding activity isn't full screen then you won't see changes in the MainActivity.
If you want to show the main activity in the background, you can use a transparent background for the sliding activity.
So, you should pass whatever params you needed from the main activity (not the activity object) using intent, use that to populate your sliding activity (designed with a transparent background).
In the sliding activity, you can save the desired params to modify the main activity when you come back to the main activity.
If your sliding activity always returns some results to the main activity, you can use startActivityForResult,
See here for implemantation: How to manage `startActivityForResult` on Android?
is really the other activity still running or is the activity in the background just a clone of the content of the previous one?
You may check this by overriding onPause and onDestroy and adding a Log Message.
If it doesn't display any message then you definitely can edit it, just pay attention to the performance.

What is the best way for keep running state of activity(or app) on android platform?

I gonna find out the way to start my app's main activty from service when the main activity is not forground running. For example in the case the activity is destroyed while executing for crash, need to start activity again. I 'd like to create one service for it to check my activity's running status. if not running my activity my service would start the main activity again so always my activiy keep running status except to be finished by user. What is the best way? Kindly recommend me. Thanks.
If your activity have multiple instance, you can add an static variable such as static int activeInstance = 0; or place it in your SharedPreference and increase it every time an instance of the activity is called.
If you are sure that the activity can only have on instance running at a time
you can place android:launchMode="singleInstance" inside that activity in the Manifest.xml file. You can read in more detail here http://developer.android.com/guide/topics/manifest/activity-element.html
Hope that help :D

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.

Activity is stopped when starting new activity with a custom Camera implementation

When starting a new activity that has a custom Camera implementation, the main activity is closed (onStop is called with IsFinishing() set to true). When calling other activities this does not happen. I am working under the assumption that the main activity is being closed due to a low memory condition, as I can start other activities without error. How do I prevent the main activity from being shutdown when I call the camera activity, as there is a service started in the main activity that will be re-used for the camera activity?
Code that calls new activity:
startActivity(new Intent(Context, MyClass.Snapshot.class));
Try using startActivityForResult to signal to Android that you want your Activity to be delivered a result.
I don't think keeping the MainActivity from closing is a viable option. You stated that the reason is related to the MainActivity starting a service. Well really anything with a a reference to a Context can start a service. You could use a Singleton quite easily. I think keeping the Main Activity around is not necessary, and not a good practice since Android can always decide something like this. One thing you might try is to startSticky the Service and see if that makes a difference. Let us know.
The answer is my own stupidity. Had a bunch of commented code below my startActivity call, but I forgot to comment out 1 line towards the bottom... That line was calling onFinish() which would explain everything.
Thank you everyone for trying to diagnose my stupidity.

How to ignore Activity life cycle in Android?

I just want to ignore the Activity life cycle.
I mean that I want to make the Activity is always on top of the window.
The Activity is not be destroyed even if the other Activity or App is executed.
No, you can't do that. It violates the Android Framework.
If you just want to do something all the time, in the background, you should use a Service.
Your words are really contradictory.
You want to ignore Activity life-cycle, which means you wanna run wild out-of-the-box
The Activity is not be destroyed even if the other Activity or App is executed. <--- yes, of course, from Activity A you start Activity B, Activity A is still alive according to the Activity life-cycle: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
You can't do that without changing the Android Code.

Categories

Resources