Change main activity in code - android

How to change main activity at application startup in Android?
I want to implement lock screen asking for password, before allow user to interact with application. Password screen can be turned of in application's settings and in such case (when application should not ask for password) I want to start Main application activity not Password activity. How it is possible?

In the onCreate of the Main activity, check preference and if password is on startActivity to load the Password activity.

First, I am new to Android programming so bear with me!
If I was doing it, I would have created another activity that would be the main activity (set in the AndroidManifest.xml file) and inside the onCreate() event I would have checked that parameter in your setting file and depending on its value, I would have launched the password activity or the other one.

Related

Activity behind the passcode is shown in the activity stack

I have two activities - one for passcode and the other one behind the passcode activity. The flow is usually when the app is launched the passcode screen/activity is shown first and the main activity is shown next. But, when the app is on the main activity (not the passcode activity), and the app is minimized, the main activity is put in the stack. At this point, if the "recent list" is launched, the main activity is shown, not the passcode activity. Is there any way this can be solved? Or, is this how Android behaves?
Can someone please help me with this?
Thanks in advance.
Yes, this is how Android behaves, but there may be a solution.
If I understand you correctly, you want the user to always have to enter the passcode before seeing the main screen, whether when launching the app initially, or any time they leave the app and return.
For that, one approach would be to use one Activity with two Fragments. Whenever the Activity is restarted, load the passcode Fragment. When the correct passcode is entered, switch to the main Fragment.
This should work for you. You can use the following code in the main activity
#Override
public void onPause(){
super.onPause();
// put your code here...
this.finish();
}
This will remove the main activity fromthe stack every time the app goes in the background.
Also make sure that you don't finish() the Passcode Activity when you startActivity to the main activity.
eg: (in your Passcode Activity)
Intent i = new Intent(PasscodeActivity.this, MainActivity.class);
startActivity(i);
Okay, so... How about launching the passcode activity? Then from there you'd be able to launch the main activity, and finish the passcode activity. This way the main activity isn't launched until the passcode is actually entered, and it'll help you keep the responsibilities separated. Seems like the easiest solution to me.

How can I configure Launcher activity programmatically in android?

I am working on an app with two activities : LoginActivity and MainActivity. When the user first opens the app he will login and his credentials (username and token) are saved in Preferences.
Now, if the user opens the app again then MainActivity should start. I tried to switch between these activities in Application class and removed intent-filter for LAUNCHER_ACTIVITY from manifest, but it doesn't work.
Is there any way of switching between Launcher Activities programmatically on basis of saved preferences?
Is there any way of switching between Launcher Activities programmatically on basis of saved preferences ?
You can try this:
Step #1: Have LoginActivity have the LAUNCHER <intent-filter> as normal, and have MainActivity have no <intent-filter>.
Step #2: Have an <activity-alias> element in the manifest pointing to MainActivity that has the LAUNCHER <intent-filter>.
Step #3: Put android:enabled="false" on the <activity-alias>, so it is disabled by default, so when the app is first installed, the only launcher icon is for LoginActivity.
Step #4: When the user logs in, and you want to change so MainActivity is the launcher activity, use PackageManager and setComponentEnabledSetting() to make the <activity-alias> enabled and to disable the LoginActivity.
Not all home screens will detect this change on the fly, and for those, the device would need to reboot in all likelihood to pick up the change. For this reason, it would be better to stick with a single launcher activity. If you want, that launcher activity could have Theme.NoDisplay and simply route to the correct "real" activity in onCreate(), per Fahim's answer.
Long story short, you cannot change the Activity that is launched by default. Update: There is an alternative as described by CommonsWare in another answer.
However, there are reasonable work arounds. In your MainActivity you can check whether the user is logged in and immediately redirect them to the LoginActivity. That has the added benefit of automatically returning to the MainActivity after you have logged in.
Alternatively, you can always go first to the LoginActivity, and if the user is already logged in, send them to the MainActivity (rewrite the Intent history to remove the return to LoginActivity or set the noHistory flag in the manifest).
The easiest way is to make MainActivity launcher activity, as usual.
Then check in MainActivity#onCreate(Bundle) via SharedPreferences if the user already logged in and, if not, start LoginActivity immediately. When user logs in, save the boolean flag indicating that user logged in in SharedPreferences and finish MainActivity.
An activity doesn't necessarily require a UI, so you can use the launcher activity in the manifest to lauch any activity you desire.
As far as I know changing launcher programmatically is not possible, but it also doesn't make sense.
On your LoginActivity's onCreate check if a username and token is already saved, if it is try to login with that automatically, is succeed redirect to your MainAcivity. Depending on the way your app works you can have a variable that checks if a user is logged in or not, if he is the LoginActivity would redirect him to MainActivity without trying to log in again.
//LoginActivity
onCreate(Bundle bundle)
{
/* ... */
//Or whatever you use to login (it could also go inside a thread or an AsyncTask
if (login())
{
//Intent
Intent intent = new Intent(this, MainActivity.class);
//Start Activity
startActivity(intent);
//Finish this activity, so when user pressed back the login activity will not come forth and the app will exit
//this looks like when a user has logged in once, the login screen will not be visible to him (unless you want to)
finish();
}
}
You can also configure it to save username and token only if a login is successful which means the above code can be modified like this:
if (getUsername() != null)
{
/* Start Main Activity */
}
This won't attempt to log in, but it knows the credential are right since it has logged in at least once with them.
If your app behaves a different way that these methods do not work, feel free to say so, I may be able to provide more info
You can jsut add Intent after OnCreate to the XML you want to show in the beginning of your APP.
public class LoginActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//add some code to detect if user is logged in
if (user != null){
Intent in = new Intent(LoginActivity.this, YourDesiredActivity.class);
startActivity(in);
}
..........
...........

Is there a way to detect which Activity will be resumed upon hitting the back button?

My app begins with Activity A (log in screen) which I always want on the bottom of the Activity stack.
Based on some logic after login, I launch some other Activity B-Z. There is a menu in the app which allows you to switch around between any Activity B-Z.
If the user hits the back button enough times, I don't want them returned to the login screen. I want the user to be sent to the Android Home screen if the back button is pressed on the Activity which has Activity A as the next Activity on the stack. This Activity is not guaranteed to be the Activity which was launched by Activity A because my Activities use singleTop.
Ideas?
The only other option I can think of is to remove singleTop and whatever Activity is launched by Activity A could remember that (my Activities all derive from a base class, which I would use to do that).
Another possibility may be to do something like the following in the onBackPressed handler:
if (getParent().getClass().getName().equals(ActivityA.class.getName())) {}
Although not a direct answer to your question, but if the problem is that
I don't want them returned to the login screen
then the classic solution is to finish() the login Activity when the user has logged in successfully. This way you'll make sure the user will never return to that Activity.
To do this, Why don't you get the User Login information, and store it in your apps private shared preferences. Then when the User launches your application, you can read the Shared preferences from activity A, and automagically log them in to activity b .
something like this:
SharedPreferences myPrefs = getBaseContext().getSharedPreferences("MyPrefs", SharedPreferences.MODE_PRIVATE);
if((myPrefs.getString("username",null) != null) && (myPrefs.getString("password",null) !=null)){
// Make sure your user is a member of your application
// auto log in to the home page
Intent bIntent = new Intent(getBaseContext(), BIntent.class);
startActivity(bIntent);
}
For more reference on how to use Shared Preferences
http://developer.android.com/reference/android/content/SharedPreferences.html
Then if you want to be really slick, Store the Class name when the onDestroy() method is called in each Activity, overwriting each class as the last open Activity. So whichever activity the user was last on before the application was closed is stored in preferences and you can read that in the login activity, then from there launch b-z
This will make it so that the login activity is always in memory, since it is first launched to check your users credentials.

How would one design a flow where one of the screens is a login screen?

I am writing an Android app which requires the user to be logged in. I have a main Activity, and I want that when the app launches, that it takes the user to a login screen if they are not already logged in.
How should I model this? Is the login page another Activity? I'm thinking not because I don't want the user to be able to get back there using the back button. Or is there a way to replace an activity with another so the back button problem doesn't happen?
Or, should I have just one activity and load in a login view and swap it out once the user logs in?
Are there any best practices around this?
Thanks!
I would check on the main or splash screen to check if the user is logged in, if not start the login activity.
Once the login completes, in the login activity, call this.finish()
If you need to change activities, you can call .finish() followed by starting whatever activity you wanted.
If getting back to the splash/main screen without being logged in is a problem, you can do the same thing there.
For this ask "I'm thinking not because I don't want the user to be able to get back there using the back button"
The answer is using the attribute noHistory in your Login Activity, like the example:
<activity
android:name="com.test.LoginActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:noHistory="true"...
I hope it can help.
Yes, the login screen is an another activity. The back key problem is solved with onKeyDown() method of that activity. Read more here.
The whole flow can be imagined like this:
You're starting your app. The main activities check for login flag(in shared preferences or somewhere else). If user has logged in, then the main activity stays on the screen. If not, activity starts login activity using intent. Login activity performs the logging in and sets the login flag and start the main activity again.
Have you considered using a dialog for the login? I suppose what you could do is have the first Activity check at onCreate() using SharedPreferences if the user has already logged in, and if she hasn't then it generates the dialog. After logging in the dialog would change a variable which would be passed to SharedPreferences so that the user won't have to relogin if the screen orientation changes or the app pauses.

dynamically launching activity in android

I am developing an application where one of my Activity contains a button as "Set as Home Page".
So my problem is that when I will click this button the status will be saved in the shared preference and next time when this application will be opened I want to start this Activity (the Activity which has been set as home page) instead of the default one.
So how can i do it???
You need to have defined static constant unique ID for each of your Activity. You save this ID to shared preferences, and implement on your boot activity's onCreate event a switch based on this stored ID against the static constant ID of your Activity. When you have the right step start the activity, and finish your current activity the booter.
You could create a kind of redirection Activity on which your application would start on. Then, put a switch in this activity, with intents sending to each of your activities, and the state of the preference would be the variable to test for the switch.
I'm not sure I'm clear but tell me if it's fine for you?

Categories

Resources