switching between Activities in Android AND passing objects - android

my android app has a lot of Activities.
As of now, I am calling Finish() each time I start a new Activity.
So my previous one is completely destroyed and when user presses the back button, I finish current activity and start a new Intent of the previous activity.
This is making my app very slow because it has to run OnCreate every time I start an activity.
If I don't finish the previous activity, how can I call the previous activity without finishing this one? User will be opening the same activities bunch of times.
I also want to use .PutExtra() when they are switching between activities.
this is what I am doing now:
Intent intent = new Intent (this, typeof(AddNewActivity));
var json = JsonConvert.SerializeObject (myDataObject);
intent.PutExtra ("myDataObject", json);
StartActivity (intent);
Finish ();
So basically, my question is how can I not start a new Activity each time and still pass data between two or more activities?
Thank you.

A simple solution would be to simply not call "finish()" every time you go to a new activity. If you follow this approach, the back-button will take the user back to the existing instance of the previous activity. Thus you get to bypass onCreate and any time-consuming operations within.
As for passing data: use Intents. When launching a new activity, call startActivityForResult. This will allow you to:
Pass data to the new Activity in an Intent.
Pass data back to the original Activity when the new Activity is done.
See: http://developer.android.com/training/basics/intents/result.html

Related

Using an Activity to Launch Either Login or Main Activities

I have an application which currently launches a simple LaunchActivity upon starting. Within this activity, there is a conditional in onCreate() to check for the existence of session data. If no session data exists, it starts the LoginActivity. Otherwise, it starts the MainActivity. In both cases, it finishes the LaunchActivity before starting either activity.
Using this approach, there is a brief flicker of the LaunchActivity before the start of either other activity. If this is an appropriate approach, what can be done to eliminate the flicker?
Is there another approach to this behavior which does not involve a LaunchActivity?
it finishes the LaunchActivity before starting either activity.
This sounds like you are destroying your Activity, then launching a new one. What you can do instead is open the new Activity using Intent then finish the launch Activity:
Intent intent = new Intent(this, MainActivity.class);//or LoginActivity.class
startActivity(intent);
finish();
Additionally, you can specify in your manifest that you do not want the launch activity to be included in the Back Stack. This will make it so that when the back button is pressed to exit the main or login activities, the launch activity will not be shown.
android:noHistory="true"

Android - Out of Memory Error due to starting a lot of Activities started

I have an application that implements somewhat like having a top page.
So like, i have this activities:
TopActivity , FirstActivity, SecondActivity, ThirdActivity and FourthActivity.
Each activities has a button that when you press it, it will bring you back ti the TopActivity.
The way I implemented this one is every time that button is pressed, I start an activity, a new TopActivity. So, every time a top activity button is pressed, it always create a new activity. If I also will finish() the TopActivity when it goes to first, second and third, I can not go to TopPAge using back button. Are there itger ways to do this?
Any help is greatly apprieciated.
You can use a different Launch Mode for you activities. The launch modes decides when and how to create a new Activity or reuse a previous one.
The singleTask launch mode seems like it would do the trick for you:
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to existing instance through a
call to its onNewIntent() method, rather than creating a new one.

How to launch the activity of this case?

I have 3 activities in my app.
Home -> List -> Detail
When I am in Detail activity, I want to have a home button that will redirect me to Home activity and close the other activities. Then, I tried this code:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
since the FLAG_ACTIVITY_CLEAR_TOP is:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
I got what I want, but then it still goes to onCreate. Is there any way to launch the home and not going to its onCreate?
Your thinking and Android's are not yet fully compatible. ;)
When you "launch the home", that means it's going to be launched, and that means it's going to be created.
You can, however, use Activity.startActivityForResult() to start List and Detail. Your "home" button then will finish your Detail Activity. In the onActivityResult callback of List, you'll just pass through using Activity.finish(), and in onActivityResult of Home you'll just catch the event and know that you're now back.
You cannot control the Activity life cycle. It's up to Android to decide which activities to keep running and which to close. This means that when an Activity loses focus, it is not necessarily destroyed or closed, but might simply lost focus and keep running, but might also not. This is up to Android; you cannot control this behavior.
If you want to recover an Activity in a given state, you have to save all needed data and restore it in onCreate().

Activity struck while pressing back key

There is no action performed on the activity when i press back key till some time. No code return to handle back key in my activity. I am starting a activity when user clicks on list item. Like
Intent intent = new Intent(cxt, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Actual thing hapening is,when user clicks instantly thrice,My activity is launched multiple times.How can i avoid launch activity more than once.
I can not use single instance/singleton because my OnCreate should be called every time the activity is launched.
I would assume that you do extensive processing in one of the Activity lifecycle callbacks which Android calls in this case to destroy your Activity.
Decide wether you start an activity or not outside the activity itself, and consider checking conditions there. You can pass parameters to your new intent based on the conditions.
here is a question on that topic: How to start an Intent by passing some parameters to it?
you can still use singleInstance and override onNewIntent method which will be called on every relaunch of activity , or separate expensive operations in handler post methods .

How can i manage my activities and finish them properly?

I am new in android and I have total 6-7 activities in my application. I want to know how can I manage my activities properly means when I move to the A->B->C->D like that. Then how can I move that the stack of these activities not created.
On moving from one activity to the other I am using the below code:
Intent intent=new Intent(current.this,next.class);
startActivityForResult(intent, 0);
And now if I want to move back on the earlier activity I used the code as:
Intent start = new Intent(current.this,next.class);
startActivity(start);
finishActivity(0);
Is there a special reason that you don't want to use the activity stack and let the activities handle themselves?
The Android system has done a very good job with the activity lifecycle. It allows you to start an Activity from different places without confusing the user because the back button will bring the user back to a different activity.
If you don't have a very good reason to not use the Android guideline try to stick to the way the system is doing it. Every other thing will only give you problems.
You are starting activities for a result but how I understand you you will never return to them.
You can start an Activity and after that just finish the current Activity. That way the activity will not be put on the back stack. Now you need to listen for back button pushes and create the activities that you want to bring the user to.
If you want to move from Activity A to D like going to the start/home screen of you app you do the following:
Intent goBackToA = new Intent(context, StdActivity.class);
goBackToA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(goBackToA);
The flag FLAG_ACTIVITY_CLEAR_TOP will tell the system that if the backstack contains an instance of the Activity this activity will be shown and all activity that are between the current activity and the target activity are removed from the backstack. This allows you to go back to a home activity without creating huge loops that the user can move through with the back button.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapear.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapeear or you can press Back button to see the previous Activity .
whenever you want to navigate from one class to another use this code, may be this help you to navigate the Activity,
Intent nextpage = new Intent(CurrentActivity.this,NextActivity.class);
startActivity(nextpage);
this.finish();

Categories

Resources