Activity Lifecycle and the stack of activities - java

I have an activity with the following screen scheme:
|------> Activity1
MainActivity |------> Activity2
The application navigation only use startActivity() calls and the standard "back button" function.
Considering an Activity as created when its in between the onCreate and the onDestroy methods.
May I assume that when the activity1 or 2 is created then the MainActivity is created too?

There is no guarantee that the MainActivity will still be alive when you have Activity1/2 in the foreground. Definitely it will be created when the app starts since it is the only way to reach to Activity1/2.
Once the MainActivity is covered by another activity it will be in Stop state and can be killed by the system when resources is needed.
You can have some control over this behavior by specifying android:noHistory="true/false" in your activity definition, which by default set to false

Yes, when Activity1 or Activity2 is created, then by your definition MainActivity has also been created. So, if you are trying to access variables or methods from within MainActivity, then will be there.

Related

Where is the difference between onCreate and onStart if both are called upon Activity change anyway? What's the purpose?

I've searched through dozens of Stackoverflow posts and the android doc but just couldn't find the answer.
According to the accepted answer of this SF-post the onCreate method runs when the activity is first created. It also notes that in here views are supposed to be created and list data is being binded.
Then the onStart Method runs but here's the issue. Where's the difference? If you do everything inside of onCreate, switch activities, your app will still display the same data, regardless whether you put the app in the background or switched activities.
So if you declare views in onCreate, what do you do in onStart? initiliaze the views to their R.id.view ? Fetch data?
onResume I suppose is then used for listeners since it's the gas and brake according to this SF-posts accepted answer.
onCreate() is called when the activity is first created. onStart() is called whenever the activity becomes visible, which includes when it is first created (after onCreate()) and after it is coming back to the screen from being stopped (e.g., another activity took over the screen).
So:
Put code in onCreate() that needs to happen when the activity is created (and use onDestroy() to clean it up)
Put code in onStart() that needs to happen either when the activity is created or when the activity returns to the foreground (and use onStop() to clean it up)
Frequently, we do not do anything special when the activity returns to the foreground, in which case you do not need to worry about onStart() or onStop().

Can use the intent-filter in android to start a method

me and a partner are working on a project and have designed an app that uses nfc, we want it so that when you're off the app you can tap an nfc card and read it as well, the problem is we don't specifically have an activity for reading or writing a card, its triggered using a button on main activity that starts a dialog fragment, starts a class and reads the card
is it possible, using the intent filter (or any other way without having to create an activity for it) trigger a method in the activity? at the moment we just have it to bring you to the main activity but we want to start a method in main activity when that happens.
Intent filters start Activities. Activities always start with onCreate. If you want to call a single function in your activity, your code is mis-architected and that function should be in a common helper class, not in your activity.

Access fragment method from intent activity

I have a fragment, and I am starting an activity from the fragment. Now I want to call a method from the fragment in the new activity.
I tried to use interface but it seems I can't since I don't create an object of new activity in the fragment to have it call the setListener(). I am using intent to fire up the new activity.
I am not able to find how I can get fragment instance in new activity or how to call a method in the fragment. Any help would be great!
A fragment is tightly coupled with the Activity. You always need to create the Activity as the host for the fragment.
From the documentation:
A Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities. You
can think of a fragment as a modular section of an activity, which has
its own lifecycle, receives its own input events, and which you can
add or remove while the activity is running (sort of like a "sub
activity" that you can reuse in different activities).
So, you need to do the communication between Activities. An activity should not communicate with a fragment it didn't host. But it should communicate with the Activity where the fragment is hosted.
For example,
if you have two Activity which are ActivityOne and ActivityTwo. Where ActivityOne has a Fragment called ActivityOneFragment.
When you need to get the ActivityOneFragment from ActivityTwo, you need to communicate with the ActivityOne then tell it to get the ActivityOneFragment:
ActivityTwo -> ActivityOne -> ActivityOneFragment
You shouldn't do this:
ActivityTwo -> ActivityOneFragment
No, you can do not that. Because the background activity is paused/ dead. so you can not access its method.
if it is general method, you can put that method in other class. call it your utility class.

Android - Passing the main activity around

I have a several activities in my app. The main activity (activity1) extends ActivityGroup (I need to support 2.2 and above, so I cant use fragments).
The main activity1 creates activity2, which in turn creates activity3. But when creating activity3 I want activity1 to create it, not activity2.
How do you pass the main activity around between activities?
Thanks
A better way is to use startActivityForResult() in avtivity1 to statrt activity2.
and when you want activity3 just finish activity2 and in activity1 override
onActivityResult() and start Activity3. passing activity instance is not better idea
since that Activity may get killed when at background and thus susceptible to throw an
Exception.

Prevent an Activity from being killed by the OS while starting a child activity

I have a main activity which calls a child one via
Intent I = new Intent(this, Child.class);
startActivityForResult(I, 0);
But as soon as Child becomes visible the main activity gets its onStop and immediately after that onDestroy method triggered. And as soon as I call finish() within the Child activity or press the back button, the Child activity closes and the home screen shows (instead of the main activity).
How can I prevent the main activity from being destroyed? :\
If you launch a child Activity from which you expect return data, you'll probably want to use startActivityforResult instead.
You may want to check this question: Child Activity in Android as it seems to be the same problem.
Edit:
As for what's happening here, you could place code in the onStop() and/or onDestroy() methods to investigate - at least a call to isFinishing() to check why the Activity is being destroyed.
You should also use adb logcat from your host machine to check the logcat in case it holds more information - and maybe use Log.d() (the result goes into logcat as well) instead of toasts to make sure you don't miss them.
I used Dialog instead of an Activity and everything worked well so I'm leaving it like that.
check androidmanifest nohistory=true and that made the OS destroy the activity before the result. that might be one of the reason for your problem.

Categories