I have a question that I've been looking for answer to for several days with no luck.
So, to be as specific, I have TabActivity inside TabActivity and listview inside it (I call this entire screen "main screen"). That listview is populated from data sent from my database. Upon clicking an item of the listview, I start an activity but upon exiting that activity and coming back to the main screen, the listview is recreated and I was wondering whether there was a way to stop from being recreated.
Which method do I need to override to simply start and finish activity without destroying the main screen?
Edit--
I don't have FLAG_ACTIVITY_CLEAR_TOP attached to any of the activity
Related
I am working on an android application. I have cardviews for multiple fragemnts. I have put these cardviews in the main activity. So when I pressed the cardview a fragment will open. So i will make these cardviews visibility as gone. But on back press the onResume() is not called. Hence the layout is not getting visible. I tried many things but did not work. What should I do?
From what I understand, you have an activity with a fragment. Then you load a new fragment and then you press back. This interaction will not fire the activity's onResume() because the activity has never been stopped. If there is logic you need to perform between fragments, it needs to be somewhere else. I cannot provide any more details without some more information.
I think first of all use the navigation on Android Jetpak
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'm a total beginner with Android and Eclipse and I have few questions that will help me understand the philosophy of Android:
The activity does all the work "behind the scenes". Activities are connected to a layout in the XML file. When the activity is started, if declared in setContentView method, the connected layout will be shown. Activity can be independent, without any layout, it can be invoked by another activity and will do all the work without showing any layout.
Activity is something like a php file which is invoked by my submit button in HTML, and the layout is .HTML which shows elements.
Am I right with this one?
For example, if I want to change the layout of my app, I want to show Layout2.xml when clicking button in Layout1.xml. Then I have to destroy the activity which is connected with Layout1.xml and start the activity which is connected with Layout2.xml? Is this correct? Is there any better way to do this?
How can I (by which method) destroy/stop a certain activity?
Thank you in an advance.
The best bet is to read the Android documentation regarding activites at http://developer.android.com/reference/android/app/Activity.html
I will answer your specific questions here though
An Activity is a window that the user can see (or a hidden window if there is no layout defined). It deals with the logic of a part of the app that the user can see and interact with. If we take the MVC model (Model View Controller), the Activity is the controller, in terms of it controls which data from the Model is shown on the View (the xml layout).
If you want to show a new window/screen/activity you do not need to destroy the current one. You can open a new activity whilst keeping the old one in the background (in the back stack). With the use of fragments, you can have multiple fragments in an activity so rather than changing activities, you can change fragments in a single activity. For more information about fragments take a look at http://developer.android.com/reference/android/app/Fragment.html.
This point relies heavily on the activity lifecycle. When an activity is destroyed, it means it is finishing and this can be done by the user pressing the back button whilst on the activity, the activity calling finish() on itself or by the Android operating system destroying the activity because memory is required elsewhere (this can happen when the app is in the background).
When we say an activity is stopped, it means that the activity is no longer visible to the user. This can be the case where the activity is in the back stack (another activity is in front of it) or if the app has been put into the background.
This is a brief answer to your questions but I highly recommend you read the Android documentation to gain better knowledge.
In my application i am using tabhost. And to control different activities i am using activity group. I have Activity A from which i am going to activity B. Activity B contains Edit Text, Spinner, Button etc. Now when i scroll the Activity B and press the device Back Button than it does not go to the previous Activity. It goes out of application.
Kindly suggest the answer.
This is the way the Activity lifecycle works for Activity Group. Try using fragments instead of activity group with your tabhost to get your movements. Also, please edit your question title to more accurately reflect the actual question.
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