Android: Using the app home page as launcher activity - android

I am building a simple facebook login application in android. I am using my login activity as the launcher activity. However, I would like to use my home page as launcher activity if the user is already logged in, otherwise login activity act as the launcher activity. Can anyone tell me the right way to do that?

If the user login, save that data, could be a string value, int, boolean, etc... check the data and if matches, make a staractivity intent.
Use my library to achieve that: KeySaver
In your Login Button (onClickListener) make this:
mLoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!KeySaver.isExist(this, "haslogin")){
KeySaver.saveShare(this, "haslogin", true);
}
});
And at the very beginning of your Login Activity make this:
if(KeySaver.isExist(this, "haslogin")){
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(myIntent);
}else{
// do your code for login, etc
}

When logging in first time do this:
SharedPreferences shared;
shared=getSharedPreferences("com.example", Context.MODE_PRIVATE);
shared.edit().putBoolean("loggedin",true).apply();
After you are already logged in
in Your onCreate() of loginActivity do this:
SharedPreferences shared;
shared=getSharedPreferences("com.example", Context.MODE_PRIVATE);
if(shared.getBoolean("loggedin",false)){
startActivity(new Intent(LoginActivity.this,Homepage.class));
finish();
}

Related

can I change launcher activity in MainAvtivity

How can I change Launcher activity in android studio using Button click in Java file??
for example, if user clicks Button1 then a activity will be launcher activity and if user clicks Button2 then another activity will be a launcher activity???
Which activity is launched at startup is determined by the AndroidManifest.xml file, you cannot modify this at runtime.
However, you can make an activity whose sole purpose is to launch the app and then choose which other activity to launch:
in button 1 click, save activity 1's name into a sharedpreferences entry
in button 2 click, save the other's name
in the launch activity, check the value of the sharedpreferences entry and launch an intent with the correct activity based on that
Edit: example for what you are trying to do
In your Button1 click:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// get the sharedpreferences with the name "myPreferencesName"
// and the mode "MODE_PRIVATE" (i.e. only you can read the settings stored in it)
// and then get its editor
SharedPreferences.Editor editor = getApplicationContext().
getSharedPreferences("myPreferencesName", Context.MODE_PRIVATE).
edit();
editor.putString("activityToLaunch", "activity1"); // activity1 can be whatever string you choose, not neccessarily the actual name of the activity
editor.apply();
}
});
In your button2 click, you do the same but put "activity2" as the preference value
In your MainActivity onCreate:
SharedPreferences prefs = getApplicationContext().
getSharedPreferences("myPreferencesName", Context.MODE_PRIVATE);
if(!prefs.contains("activityToLaunch")) {
// neither button has been pressed since you installed the app
// this always happens on the first launch after installation
// do the editor thing here as you would in the button click
// depending on which activity you want to be default, either set the preference value to "activity1", or "activity2", then editor.apply() it
}
if(prefs.getString("activityToLaunch").equals("activity1")) {
Intent intent = new Intent(getApplicationContext(), Activity1.class);
startActivity(intent);
}
else {
Intent intent = new Intent(getApplicationContext(), Activity2.class);
startActivity(intent);
}

Launch an activity only once after Install

I have QR scanner app. There are 3 activities in the app.
1) Main activity - Button to open camera and start scanning
2) QR activity - Scan a QR code
3) Web Activity - On successful scanning, open a web page in the app
Here, the Main activity and QR activity should only launch once, only after the initial install. I read somewhere about using shared preferences. But I am a little confused as to where do I check the variable, as in which activity. Should I check my shared variable in the Main Activity?
This is my first app. Sorry if this is a silly doubt.
It's correct, you have to do it with SharedPreferences.
Here is a good explaination about how to use them
On the first activity shown, you have to add in the onCreate method those lines:
//this retrieve the sharedpreference element
SharedPreference myPref = this.getSharedPreferences(
"prefName", Context.MODE_PRIVATE);
//this retrieve the boolean "firstRun", if it doesn't exists, it places "true"
var firstLaunch = myPref.getBoolean("firstLaunch", true);
//so, if it's not the first run do stuffs
if(!firstLaunch){
//start the next activity
finish();
}
//else, if it's the first run, add the sharedPref
myPref.edit().putBoolean("firstLaunch", false).commit();
hope this helps
To complete #Pier Giorgio Misley answer you can put the "firstLaunch" check on your Main Activity or alternatively put it in another "splash" activity
For putting it in the main activity simply set the ui to some neutral color until you decide if you should finish the activity and launch the Web Activity or show the Main Activity logic
Alternatively, you can create a "splash" screen which can function as a bridge activity (which shows some logo or a nice background color) which check the varible and decide which activity to open Android splash
As pier mentioned, saving that it has been seen once is the way to go. However, I have found on some older devices, shared preferences is not reliable!
I recommend instead using SQLite database.
Create a table as follows
TABLE NAME: SEEN_ACTIVITY
Column 1: ID INT PRIMARY KEY
Column 2: SEEN VARCHAR
Then, once the activity has been launched, check if there is a record for id = '0' in SEEN_ACTIVITY. If not, then insert one as follows, (0, true).
Then, every time the app launches, check to see if the record exists of (0, true). If not, launch the extra activity.
My MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
SharedPreferences settings = getSharedPreferences("prefName",MODE_PRIVATE);
boolean firstLaunch = settings.getBoolean("firstLaunch", true);
if(firstLaunch == false) {
SharedPreferences.Editor editor=settings.edit();
editor.putBoolean("firstRun",true);
editor.commit();
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
}
else {
Intent i = new Intent(SplashActivity.this, ScannerActivity.class);
startActivity(i);
finish();
}
}
}, SPLASH_TIME_OUT);
}
Wrote this code in a new Splash Activity

Opening a different activity once the user Logs In [duplicate]

This question already has an answer here:
Android: creating a "first time user" activity
(1 answer)
Closed 7 years ago.
I want that when the user opens the app for the first time after installation he gets an activity which asks him to make account. Once user makes account and he gets Homepage of his account. Now he closes the app. Opens it the next time. He should get the homepage and not the make account activity again. How can that be done?
Edit: I want to know where exactly i need to check my shared preferences. In the main activity's onCreate?
Create a preferences object and store the status of registration in it. Create an activity to register/make an account. Once account is made, store this in the preferences object and check this preferences object when the app is opened. If its true, they registered and show the home page, if its false show the 'make account' activity.' You could alternatively use a subclassed 'registration dialog' in the main activity instead of calling/writing another activity all together.
First,Store user details on SharedPrefrences
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putBoolean("logged",true)
editor.putString("username",username);
editor.commit();
Then you can choose either to make a Dispatcher Class like this
public class Dispatcher extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPreferences = getSharedPreferences("prefs",MODE_PRIVATE);
if (sharedPreferences != null) {
if (sharedPreferences.getBoolean("logged", false)) {
startActivity(new Intent(this, MainActivity.class);
}
} else {
startActivity(new Intent(this, LoginActivity.class));
}
Or just implement this on the onCreate() method in LoginActivity

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

Android MainActivity calls another Activities, after pressing back key, blank screen shows

my main.xml file is just dummy. I want to start different activites based on the condition. If the password is found in shared pref file, the login activity should be launched, and if password is not found, the configuration activity should be launched. it is working fine but when I press the back key from keypad, the main activity is shown (I mean the blank screen because there is nothing) How can I avoid this?
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = getSharedPreferences(preffilename, MODE_PRIVATE);
final String password = prefs.getString("password",null);
if(password == null)
{
Intent i = new Intent(getApplicationContext(), Configuration.class);
startActivity(i);
}
else
{
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
}
}
Call finish() from your main activity after calling startActivity(), this will remove main activity from stack.
What is your expectation when you pressed the back button? You might want to put those code in onResume() so it always get called when the main activity is brought back from the stack.

Categories

Resources