When good time to start another activity - android

I have an Android Best Practice question. I have to following code, which is working nicely, but I think it is not so elegant. So, my question is: at which point of activity life cycle is nice to start another activity ?
public class LoginActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ParentPreferences parentPreferences = new ParentPreferences(getApplicationContext());
if (parentPreferences.isPassExists()) {
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
} else {
setContentView(R.layout.login);
}
}
}
The task is about: if the parent has already made a password to protect the app, than we don't need to show the LoginActivity. I don't know, is it "healthy" for an Activity to give an intent to launch, when nor the onCreate nor the other lifecycle methods completed.
What are you thoughts guys ?

I think the better way is to create LauncherActivity, and start activitys from them:
For example:
public class LauncherActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ParentPreferences parentPreferences = new ParentPreferences(getApplicationContext());
Intent intent;
if (parentPreferences.isPassExists()) {
intent = new Intent(this, MainActivity.class);
} else {
intent = new Intent(this, LoginActivity.class);
}
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
startActivity(i);
}
}
Updated:
Refering to Activity | Android Developer
onCreate is a first lifecycle method, сonsequently better to start activity B when A activity just started and does not inflate any layout

I would have the manifest start your MainActivity or whatever you call it. The MainActivity starts by checking if the user has logged in. If not, it starts your LoginActivity, which comes back in onActivityResult() with the result of the login.
It does depend on the requirement for the user to log in every time they start the app, or just once, or once in a while. If the use has to log in every time, than it's ok to start with LoginActivity. Otherwise, starting LoginActivity every time and passing to MainActivity (or whatever) seems just a waste. "Waste" not in the sense of performance, but of clarity of your app.

I think the best solution for you is to add a SplashScreen or like a "fake" screen.
Here you check if he's logged in already and based on it you start the correct activity.
Maybe the absolutely best way would be to do it with fragments, but you have to change a lot of your app.
About when to call it, the onCreate is perfect :)

Related

Start activity from another without loading the first's layout

My launcher activity may start another on a certain condition, it looks something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefManager = new PreferenceManager();
if (prefManager.startMain(this)) {
startActivity(new Intent(this, MainActivity.class));
finish();
return;
}
setContentView(R.layout.activity_wizard);
...
...
PreferenceManager is just a helper for easy access to SharedPreferneces.
If the condition is true, a flashing of the first activity layout is shown and only then the second activity starts.
I want to skip the flashing of the first activity layout when starting the second (I actually expected this since I don't call setContentView but apparently, it isn't).
I thought about creating a 3rd, transparent layout activity which starts the correct activity but I hope there is a better way.
Switch the startActivity() and finish() around so finish() is called first, haven’t been able to test but could be
if (prefManager.startMain(this)) {
finish()
startActivity(new Intent(this, MainActivity.class));
}
Shouldn’t be a need to call return either

How to be directed to page automatically?

I am writing a calculator. There are main 2 modes: polish and reverse polish system.
Anytime MainActivity is called, I like the MainActivity to direct the user to the correct page.
public class MainActivity extends BaseActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent;
String CalculatorMode = Memory.getPreference("CalculatorMode", this);
if (CalculatorMode.equals("Calc_reverse"))
intent = new Intent(this, Calc_reverse.class);
else
intent = new Intent(this, Calc_normal.class);
startActivity(intent);
}
}
After the user is directed to Calc_normal.class or Calc_reverse.class, and if he pressed the back button, he get back to the MainActivity. But nothing happen! The onCreate() is no more called. How to fix this, and is there a better way to write this? I am trying to learn, and any help is very welcome.
Instead of onCreate() you can call your methods in onStart() which will be called anytime you navigate.
onCreate() won't be called unless the activity is destroyed, you can check the activity lifecycle.
http://developer.android.com/reference/android/app/Activity.html
onStart() activity will be visible to user but won't interact.

how to go back to previous activity without re-rendering it

I have a main activity that forks a second activity, I wanted to go back from second activity to the main activity without reinitialize it. In other words, I wanted to immediately show the main activity after second activity disappears, instead of re-rendering the first activity. I have tried methods like finish, onBackPressed, none of this goes back directly achieved the effect I desired. Rather it seems that they go back and re-render the activity , which feels like all your previous data that renders the view is lost, and it has to do it from scratch again.
So How can I go back directly without re-rendering again?
Thanks
----------------------Update--------------------
Clase MainActivity extends Activity{
public onCreate(Bundle savedInstanceState){
super.onCreate(SaveInstanceState);
setContentView(R.layout.mainview);
GoogleMap gmap = ...
Route route = ...
//draw a route in the google map
// now the map bears a route on it
Intent intent = new Intent(this, secondActivity.class)
}
}
Class SecondActivity extends Activity{
public onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.secondview);
Button backbut =(Button) findViewById(R.id.backbutton);
}
//backbut is associated with a call back onClick in the xml
private onClick(View){
finish();//after this the second activity disappears, but previously drawn route on google map also disappears, and it takes time to redraw them again. feels like onCreate in firstActivity is reentered again
}
}
You need to use this two flag to accomplish what you want:--
Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_SINGLE_TOP
Just use this code:--
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
you should not call finish() on going second activity then the first activity is running on background and after calling finish() on second activity the first activity appears without re-initialize as privious state.
Hop this will help you.
MainActivity extends Activity{
Context context;
oncreate(){
context = this;
Intent intent = new Intent (context , SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
/* here finish() method is not called, if called MaintActivity objects will destory*/
}
}
Guys thank you all for your kind help! I think I kinda solved my problem. They key is that I made the onClick method private so my xml cannot find my onClick and issues an Exception, which caused activity to destroy and re-render themselves. Hope this explanation makes sense

return to the same activity, When restart the app

I have a lot of activities in my app, and I want that if the user closes the app in activity 13 for example, when opening the app at another time the activity returns in activitty n° 13. how can I do this? thank in advance
You could use SharedPreferences to keep track of the last used activity.
Then you can redirect the user in the onCreate of your main activity to the correct activity, and call finish on your main activity.
This could look something like this:
#Override
protected void onCreate(Bundle savedInstanceState)
(...)
int last_activity = getLastActivityIdFromSharedPreferences();
if (last_activity == 1)
{
this.startActivity(new Intent(this, ActivityOne.class));
finish();
}
(...)
}

Android: Store a destination activity in a bundle

So I'm working on this project, and its rather large in size. I've have put a kind of 'behind the scenes' security login system in.
Pretty much, all the activities extend the subActivity class, and the subActivity extents Activity. This has allowed me to make every form automatically .putExtra() when it starts a new activity, and on every onCreate() it will check the incoming data and determine whether it should force the user to login (push them to the LoginActivity).
So right now, the login activity, on successful login, it loads to the MainActivity. I want it to dynamically load the activity the user was last at...
This means, store the class of the activity that launched the LoginActivity in the extras, and then have the LoginActivity, on successful login, get the class from the extras and use it to start the Activity.
I'm looking for the simplest way possible, I was trying serializable, but was having a lot of issues and thought there must be another way to just pass a reference to the 'destination' class.
Hope this makes sense!
Cheers
You could use Java reflection API:
Call your login activity like this:
Intent intent = new Intent(this, LoginActivity.class);
intent.putExtra("activity", getClass().getName());
startActivity(intent);
Then parse the calling activity's name in login activity and go back to it after successful log in:
private String mActivity;
#Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// ...
mActivity = "";
Bundle extras = getIntent().getExtras();
if (extras != null)
mActivity = extras.getString("activity");
}
}
#Override public void onClick(View view)
{
try {
Class<?> cls = Class.forName(mActivity);
startActivity(new Intent(this, cls));
}
catch (Exception e) {
e.printStackTrace();
}
}
In the LoginActivity, simply call finish() which will return to the next Activity on the Stack, in your case going back to the previous one.
You can start your login activity by startActivityForResult and then check result of login activity in your main activity. Look here http://developer.android.com/reference/android/app/Activity.html#StartingActivities

Categories

Resources