How to start an app with Activity1 or Activity2? - android

I don't know how to implement this situation.
I have an app, during the start of the app, I need to calculate something and then, based on the results, start or Activity1 or Activity2.
How can I achieve this goal?
If I put the calculations in Activity1, and the calculations wants to start Activity2, however Activity1 starts Activity2 and then suddenly finish(), but the screen flickers because there is an activity that starts and ends in a few msecs. This is not very smart.

you should use a splash activity , and put your calculations there , and then based on the result you start the activity you want to

Many ways you can do that. Let me share you one example given as follows
Create New activity ( StartActivity ) which is your root activity.
Within this activity initialize some object like ( App Config, fabric, push notification, share preference data etc.)
Here you can calculate your necessary data and pass using bundle
Create Intent Object for translation to Activity1.class
if(BuildConfig.DEV_ENV && CounterPreference.getInstance().isFirstStart()) {
Intent intent = new Intent(this, Activity1.class);
startActivityForResult(intent, DEBUG_OPTIONS_ACTIVITY);
} else {
StartupManager.getInstance().start(StartActivity.this);
Logger.d(TAG, "Application Started.....*************************");
}

Add your calculation in Application class onCreate() method.And Add your activity decision in Application class.

Related

Going back to activity with EditTexts populated. - Android

I have Activity A which has editTexts and from activity A, a user can go to activity B and fill out more information. Once the user is done on activity B, they press a button and come back to A. I would like to come back to A with the information that the user had filled in to be already there and not have to fill in the information again.
Currently, this is my code for coming back from B to A.
Intent intent = new Intent(NewAddressActivity.this, CreditCardActivity.class);
intent.putExtra("newAddressEntered", true);
intent.putExtra("newAddress", (Serializable) newAddress);
startActivity(intent);
With this code, when the user comes back, the fields on activity A are empty.
*Note - I need to pass data from B to A when going back.
Any tips on how to do this properly would be greatly appreciated.
Thanks
You are on the right track. You can do that using .putExtra
You can pass extra data via the intent using intent.putExtra("key",
text_field.getText().toString()) on the intent before you send it (in
the first activity) and getIntent().getExtras().getString("key") in
the second activity.
This is assuming text_field is your EditText you want to pass the
value from. You can change "key" to whatever you want, too.
You're actually not "coming back", but starting a new empty intent, thats why the fields are blank again. Instead, you should just call:
finish()
Regarding filling related fields from different Activities, I wouldn't recommend that, but if you need to keep it that way, you may have a singleton class to store all field values for instance.
Also, you could send B fields to A as activity results.-
http://developer.android.com/training/basics/intents/result.html
But still, I would recommend keeping all related fields in one Activity.
Change the launchmode of your Activity A (CreditCardActivity) to singleTop in manifest file and try. The documentation says
If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.
So your activity should retain old values.

Starting Activities in Android

I'm a newbie in Android after Googling I found if you want to call another activity you have to use
Intent intent = new Intent(getBaseContext(), Activity_Name.class);
//if Any Extra
//intent.putExtra("", "");
intent.putExtra("EmpID", EmpID);
startActivity(intent); `
Now this is a standard approach but if you refer to the image. I'm lopping almost 20 times in the ListView (Activity1).
Now what I belive that this keeps adding again and again as my app crashes later on without any proper reason and not at any particular location.
I have disable the back button on the DataCollection Screens (Activity 2, 3,4)
Hence its a pure Waterfall approach.
Any suggestion. Should I add finish(); line in the last screen
Intent intent = new Intent(getBaseContext(), Activity_Name.class);
//if Any Extra
//intent.putExtra("", "");
intent.putExtra("EmpID", EmpID);
startActivity(intent);
finish(); `
So that it dies and goes to the previous and that dies and goes to the previous. And later landing on the ListView.
Would that be a good approach. Or is there anything where I can just call another activity like this but the system should forget all the previous activity information.
Or is there anything where I can just call another activity like this
but the system should forget all the previous activity information
Have a look at Intent Flags, e.g.FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_CLEAR_TOP.
If you dont want the user to go backwards, finish the current activity just after starting a new one.
after
startActivity(intent);
call
finish();
If you'd like to clear the activity stack, when going to a particiluar activity find it your manifest (like "Activity 1") and add the attribute android:clearTaskOnLaunch="true"
more info here : http://developer.android.com/guide/topics/manifest/activity-element.html#clear
You are probably better of using startActivityForResult rather startActivity. Then, when you call an activity you know that when it finishes it will return to the Activity which called it (it will come into onActivityResult). This way you can achieve:
Activty 1 -> Activity 2 -> Activity 1 -> Activity 3 -> Activity 1 etc
or
Activty 1 -> Activity 2 -> Activity 3 -> Activity 2 -> Activty 1 etc
In both cases, you don't can keep "passing through" phases of an activity invisible, they can just receive the "activity you called" trigger and pass on to the next activity.

From within an android activity test, how do I finish another activity that my main activity started?

I am writing an activity test for an activity we wrote with 3 buttons. 2 of these buttons start other activities.
I can write a test that simulates a button push and then checks if the desired activity is running, but I can't move back from that second activity. The second activity stays at the front and prevents the other tests, that assume the first activity is running, from working properly. They just kind of freeze.
I have a reference to the first activity, but it is the second activity I need to I guess call finish() on. Is there a way to do this?
EDIT: I added some actual source code illustrating my problem in this gist: https://gist.github.com/3076103
It is specifically about testing activities. In the production code everything is fine.
You should probably use http://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor.html to get a reference of the second activity or you can block the second activity from being launched(Still you are guranteed that the call to start the second activity infact had reached till the framework).
You need a way for your activities to communicate with one another, so that one activity can tell the other to finish. There are several ways you can accomplish this. One method is to create a service within my application; my "second" activities would connect to this service to register a way to receive messages, and my primary activity would connect in order to provide them.
In Activity1 add the following to start Activity2
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
In Activity2 add the following to start Activity1 and finish Activity2
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
For more details: http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/
If you start the activity using startActivityForResult, then you can close the activity from parent using finishActivity(int requestCode).
To start the activity :
startActivityForResult(new Intent(...), 123123 /*requestCode*/);
And when you want to finish that activity (from caller), use :
finishActivity(123123 /*requestCode*/)
Also there is a way to find, whether child activity is finished or not. But you can track this only when child activity calls finish() for self. To receive the child finish request from the child, you need to override the finishFromChild() method in parent activity.

Basic Android code flow

Lets say you have one main activity called "main", and other sub-activities that do some work and then return some data. Each time main gets called it starts up the sub-activities one at a time.
Main:
onCreate...{
Intent i1 = new Intent("com.bla.bla.activity1");
startActivity(i1);
//Get bundles from activity1 and save some variables..
Intent i2 = new Intent("com.bla.bla.activity2");
startActivity(i2);
//Get bundles from activity2 and save some variables..
Intent i3 = new Intent("com.bla.bla.activity3");
startActivity(i2);
//Get bundles from activity1 and save some variables..
}
If I understand correctly, activity1 will be created at first, and then destroyed. Will the code resume and start activity2 or will it just start activity1 again because the main activity was paused and then resumed (assuming we don't add an onResume/onPause etc..)?
Edit:
Lets say these sub-activities return one string each, and each string should be saved in one individual column of an SQL entry at the end of the main activity. The main activity should collect the three strings, save it as en entry and the destroy itself.
For this to work, the main activity has to resume for example at intent "i2" after intent "i1" has done some work and returned the string.
How is this result best achieved?
Like Alex said, you wouldn't want to do that. You want to use the different Intents to call different Activities to create a flow to your program. Main might lead to Activity 1, Activity 1 leads to 2, 3, or 4. If you have several activities, you can use a switch statement to determine which activity you want to launch, based on conditions that are, or are not, met.
Hope this helps.
EDIT Answer:
If you want to save variables from different Activities, the you would want to start the second Activity from the first, the third, from the second, and so on. To create the data for the database, you can simply create an object, and then pass that object from Activity to Activity, using the Parcelable interface. Within each of your Activities you would want to use methods to save the returned data to the database, as you see fit.

OnClick restartActivity?

How can I restart An Activity on click?
For exemple: I have got in my AndroidManifest.xml 2 activities the activity A and B and they start when application starts...
But what I want is when I click in a button that is on Activity A it must restart activity B.
when you are in activity A, and proceeding to activity B, then your activity B automatically starts/re-starts
Why do you need to start both Activities on starting your app? When you say "restart", do you actually need to stop Activity B and start Activity B again? Or do you just want to show it? To start an Activity from another Activity, you could call something like this:
startActivity(new (Intent(this, ActivityB.class)));
The Android documentation gives plenty of detail. However, I think you should consider why you are starting two activities at once, and whether you might want to use a Service instead (not knowing any details of your app, I can't say).
Intent intent = new Intent(CurrentActivity.this, ActivityToLaunch.class);
startActivity(intent);
call above piece of code on onClick of view method.

Categories

Resources