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

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.

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.

Not able to kill 2 activities at the same time. why is that?

I'm trying to kill 2 activities on the onclick of a button. The current activity and the previous activity. Using their pids. I'm just able to kill one activity. Why does this happen?
public void onClick(View v) {
android.os.Process.killProcess(pidofmain);
android.os.Process.killProcess(android.os.Process.myPid());
}
If I see in my logcat, The activity with pid "pidofmain" is getting killed whereas the current activity is not getting killed.
"pidofmain" is an integer i received from the previous activity using an intent.
Leave process killing to the OS. This is bad for any kind of program in a timesharing OS. If you want to conserve memory or something like that, let the OS handle it.
Also you can't really know if the process was correctly killed because well, if it is you wouldn't know, and if it doesn't you were not supposed to do it.
What do you want to do this for?
A much better way to do this is to call finish() for the current activity. You can also signal the previous activity to finish if it calls the current activity using startActivityForResult(Intent). The current activity would call setResult(int) before calling finish() to send a return code back to the previous activity. The previous activity can test the return code in onActivityResult(int, int, Intent) and also call finish() based on the result code.
Killing processes should be left to the OS. Once the activities finish, the will kill it off if it needs the resources. Otherwise it can let it around, which might help speed up a relaunch of your app if the user wants to run it again.
This isn't a definitive answer, but more like some thoughts that I have but it's too late for my to fire up Eclipse and prototype them. If it doesn't help you let me know and I'll try to look into it deeper tomorrow night.
A few thoughts (I hope they help):
1) Android apps really are single-threaded, and your main activity controls all the dispatch events (including events to what I assume to be a second thread that you created). If you kill the main activity, I'm pretty sure that your application would terminate execution immediately following your first call to android.os.Process.killProcess(pidofmain), and you'd never make it to your second call because you would have killed your entire application. Again, this is assuming by the variable name pidofmain that you are killing the main UI thread and not just an activity called main.
2) I'm a little curious also about where you got pidofmain? It sounds like you have three activities total, and in the first activity you get it's process id and send it to the second activity in an intent bundle, which also gets passed along to a third activity (which is the place where you're trying to kill this whole thing)? If that is the case, and you're trying to kill the currently running activity, the table in the documentation here leads me to believe that you can't just kill an activity that's in the resumed state using the same method. Official Android Docs for Activity You might want to try calling the finish() method for your currently running activity.
What exactly do you see in logcat? And what happens in the UI? Does the visible activity continue to run, but the other activity has been removed from the backstack?

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.

How do I 'navigate' to an activity that's already running?

Newbie question... So I have two activities, Cherry and Apple, and each one has a button on it to go to the other one. So back and forth.
In class "Cherry" I say this:
intent = new Intent(Cherry.this, Apple.class)
startActivity(intent);
Meaning that it should go to the Apple. There's similar code in the Apple activity.
What I think I am seeing is, is that each time when I startActivity Apple for example, it's starting a new instance of it instead of just reactivating Apple. I've scoured the doc and can't find the flag or other call that would do what I want.
Any tips would be appreciated!
-- Pito
What about FLAG_ACTIVITY_REORDER_TO_FRONT?
FLAG_ACTIVITY_CLEAR_TOP is also quite useful, finishing activities on the back stack until the target activity is reached.
To be clear, your activity may be restarted even if using the flags above. This would happen if your activity were destroyed in an attempt to free memory. In other words, you still need to make sure your activity can handle being restarted, taking the proper precautions in onPause and onSaveInstanceState.
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT will bring the launched activity back to front if it's already running as given here. You will have to handle the back button yourself so as not to finish the current activity.

android: unusual way to start activity

In one activity I need to start another one but with one condition: if it was started before then it must be finished and started again. This activity show some information about system state (Theme.Dialog style) it also can start some services and so on. As far as I know when I do startActivity(intent) then onResume() will be called (if activity was started before). Does anybody know how to do it?
That's precisely how it should work. If you need to adjust the values on-screen, put that code in onResume(). What may not be obvious from its name is that onResume() is called when the Activity is first created as well. It's always the last method called before an Activity becomes active.
use the NEW_TASK_LAUNCH flag in the startActivity() call. Read documentation http://developer.android.com/guide/appendix/faq/framework.html#4
In the manifest file, in the activity attributes, you have the attribute launch mode, which let you specify how the activity must be launched (http://developer.android.com/guide/topics/manifest/activity-element.html#lmode).
Take a look at the description to see which mode suits most your needs. Then when the activity is brought to front, you can restart your service by overriding the Activity.onResume() method.
In one activity I need to start another one but with one condition: if it was started before then it must be finished and started again
There is nothing that only does this. The closest is to have the combination of FLAG_ACTIVITY_CLEAR_TASK|FLAG_ACTIVITY_NEW_TASK, but that has other side effects, such as wiping out any other activities.
As far as I know when I do startActivity(intent) then onResume() will be called (if activity was started before).
Not by default. By default, a second instance of the activity is created.

Categories

Resources