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.
Related
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.
I am Loading all the data on splash Activity and when all calls return I want to populate another activity but keeping the splash activity until the main activity is populated.
I tried using AsyncTask but still not working, I have a delay when switching from the splash activity to the main activity.
I expect to go from splash activity to another Activity without Delay and the activity to be populated.
There are 2 solutions to your problem:
Disable animation when starting your MainActivity and try to minimize the consuming tasks in that activity.
Combine SplashScreen to your MainActivity as a View, after finishing loading all necessary tasks, just hide that SplashScreen View
You have to upload the part of your code where you are calling the intent to the other activity before we can actually understand what is wrong. However, ensure you are not doing too much activity on the application main thread since this can make your UI unresponsive and make the app slow. See this for reference =>
https://medium.com/#yossisegev/understanding-activity-runonuithread-e102d388fe93
https://developer.android.com/topic/performance/threads
start new Android Activity is so slow
You can also make your app call the new activity on a separate thread to ensure all the data it needs to pass to the other activity is received before navigating to the new activity and also ensure you are not doing too many resource consuming task in the onCreate method of the new activity you are calling
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.
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.
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