Android: Start different activity if there is a user logged in - java

I am building an Android application where one activity is a login screen. When the app is opened, if a user has already logged in, I would like to skip the LoginActivity and have the user be directed to another one. When a user logs into my app (using Google Firebase), I save their username and other data in their device's shared preferences. When they log out, their shared preferences are cleared.
The way I currently have my manifest file is such that the only activity that can be opened when the app is started is the LoginActivity:
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In the LoginActivity's OnCreate() method, if there is a username saved in the shared preferences (meaning a user is logged in), I immediately change activities:
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences userData = getApplicationContext().
getSharedPreferences("userdata", 0);
String n = userData.getString("username", "");
if (!userData.getString("username", "").equals(""))
{
Intent myIntent = new Intent(LoginActivity.this, TabbedActivity.class);
startActivity(myIntent);
}
}
However, there is a problem with this approach. Many times, the LoginActivity is still shown for a split second before starting the TabbedActivity. I would like to fix this so that the LoginActivity is actually never seen at all if a user is logged in.
I assume that the approach I'm taking is all wrong and there is a much cleaner way of doing it such that the correct activity is immediately opened. Any help on this would be greatly appreciated.

A possible approach:
Create a style for splash theme:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:statusBarColor">#color/background_splash</item>
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
Create a background drawable (drawable/background_splash.xml) for splash:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#color/background_splash"/>
</item>
<item>
<bitmap
android:src="#drawable/ic_splash_logo"
android:gravity="center"/>
</item>
</layer-list>
In your manifest, set the SplashTheme as your application/launcher-activity theme:
<application
...
android:theme="#style/SplashTheme">
<!-- or -->
<activity
...
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In your LoginActivity in onCreate() set your normal app theme and content view if the user is not logged in and also set app theme in the MainActivity (or set it in the Manifest)
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
val isUserLoggedIn = ...
if (!isUserLoggedIn)
{
setTheme(R.style.AppTheme)
setContentView(R.layout.activity_login)
}
else {
//Navigate to Main Activity
}
}
}
Splash screen reference

I'm not sure if this is the best approach, but you could create a Loading activity that start the activity needed in any situation like.
public class LoadingActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences userData = getApplicationContext().
getSharedPreferences("userdata", 0);
String n = userData.getString("username", "");
if (!userData.getString("username", "").equals(""))
{
Intent myIntent = new Intent(LoginActivity.this, TabbedActivity.class);
startActivity(myIntent);
}else{
Intent myIntent = new Intent(OtherActivity.this, TabbedActivity.class);
startActivity(myIntent);
}
}
And about the view add a gif or a logo meanwhile.

I have an implementation, but no shared preferences, and I don't see the login screen at any time.
My structure is as follows: I have an initial splash screen, then a main activty, which is where I override the onStart method to check if the user has an open section if it is passed to the home activity.
I hope it helps you, if you don't tell me and I'll add more code.
public override fun onStart() {
super.onStart()
// if user is loged goto Home Activity
if (firebaseUser != null) {
// Name, email address, and profile photo Url
val name = firebaseUser?.displayName
val email = firebaseUser?.email
val photoUrl = firebaseUser?.photoUrl
val uid = firebaseUser?.uid
val emailVerified = firebaseUser!!.isEmailVerified
goToActivity<HomeActivity>()
}
}

Related

Wrong activity displayed when implicit intent received from another app with Action.SEND

I have an app with two activities: MainActivity, which contains a URL entry field where the user can enter a YouTube video URL and press a submit button, to start the second activity, VideoActivity, which displays some information about this video (fetched from another web server).
The app also has a feature to receive intent via the Youtube application. When user presses the share button within the Youtube app, my app appears in the share list. Upon pressing share from the Youtube app, MainActivity should be brought to the front, and the URL should be posted within the MainActivity's URL field.
However, this only happens correctly on the first share. If the app is in the background when user shares from Youtube app, they are taken to whatever the last visible activity was, whether it is MainActivity or VideoActivity, (and even if it is MainActivity, the URL is not posted into the URL field, but the field is left in whatever state it was in when the app was last visible).
Here is my current AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.youcmt.youdmcapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
<activity
android:name=".VideoActivity"
android:parentActivityName=".MainActivity"/>
<service
android:name=".FetchVideoService"
android:exported="false"/>
</application>
</manifest>
Here is my MainActivity.java code:
public class MainActivity extends AppCompatActivity {
private ResponseReceiver mReceiver;
private EditText mUrlEditText;
private Button mSearchButton;
private ProgressBar mProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
mUrlEditText = findViewById(R.id.url_search_et);
Intent intent = getIntent();
if (intent.getType()!=null &&
intent.getType().equals("text/plain")) {
Bundle extras = getIntent().getExtras();
String value = extras.getString(Intent.EXTRA_TEXT);
if(value!=null)
{
mUrlEditText.setText(value);
}
}
mProgressBar = findViewById(R.id.progress_bar);
mSearchButton = findViewById(R.id.search_button);
mSearchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
askForVideo(mUrlEditText.getText().toString());
mSearchButton.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
} catch (Exception e) {
mUrlEditText.setText("");
mUrlEditText.setHint(e.getMessage());
e.printStackTrace();
}
}
});
}
#Override
protected void onResume() {
super.onResume();
//register the ResponseReceiver
mReceiver = new ResponseReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(FETCH_VIDEO_INFO);
registerReceiver(mReceiver, intentFilter);
}
private void askForVideo (String url) throws Exception {
try {
Intent intent = FetchVideoService.newIntent(this, url);
startService(intent);
} catch (Exception e) {
mUrlEditText.setText(e.getMessage());
}
}
public class ResponseReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(EXTRA_VIDEO_STATUS, FAIL);
mProgressBar.setVisibility(View.INVISIBLE);
mSearchButton.setVisibility(View.VISIBLE);
if(status==FAIL)
{
mUrlEditText.setText("");
mUrlEditText.setHint("Error retrieving video!");
}
else if(status==SUCCESS) {
Video video = intent.getParcelableExtra(EXTRA_VIDEO);
Intent videoActivityIntent =
VideoActivity.newIntent(getApplicationContext(), video);
startActivity(videoActivityIntent);
}
}
}
#Override
protected void onPause() {
unregisterReceiver(mReceiver);
super.onPause();
}
}
I do not think any of the other files will be useful in understanding the problem. Although this seems like something many app creators should have to deal with, I can find no answers to this problem. Please comment if you feel I should add any additional information and thank you in advance for any help!
Update: testing demonstrates that after the first use of "Share" from YouTube (and considering app remains in the background), the MainActivity no longer receives any new intent on further shares. However, my app is still brought to the foreground somehow. This is very confusing to me.
When you share from another app, your MainActivity is brought to the front and onNewIntent() is called on it. You don't override onNewIntent() so you never see the share Intent.

How to direct user to its specific activity?

I am building an app in which when the user first installs it, In the MainActivity(Launcher Activity) the app asks what type of user he is(Let's say Driver or Rider)? Based on the user selection he is directed to the corresponding login screen. After the user is logged in he directed to HomeActivity. If he closes the app without logging out, next time he opens the app he should directly see the HomeActivity(based on what usertype he selected). For one user the code in the MainActivity would be like:
#Override
protected void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null) {
sendToStart();
}
}
private void sendToStart() {
Intent startIntent = new Intent(MainActivity.this,DriverHomeActivity.class);
startActivity(startIntent);
finish();
}
I am new to Android and Firebase. I don't how this should be done for two types users.
Create one login screen, in that screen the user can choose work type(two buttons or radio buttons).
If he chooses Driver, then create a Driver node in the database with the information he wrote and direct him to the home activity:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Drivers").child(user.getUid());
ref.child("name").setValue(name);
Intent startIntent = new Intent(MainActivity.this,DriverHomeActivity.class);
startActivity(startIntent);
finish();
Also do the same for Rider.
If the user, closes the app without logging out. In the first activity that should open (ex: splash activity), check if there is a current user and check type of user:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference driver = FirebaseDatabase.getInstance().getReference().child("Drivers");
DatabaseReference rider = FirebaseDatabase.getInstance().getReference().child("Riders");
if (user != null) {
driver.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Intent i = new Intent(SplashActivity.this, DriverHomeActivity.class);
startActivity(i);
finish();
} else {
Intent intent = new Intent(SplashActivity.this, RiderHomeActivity.class);
startActivity(intent);
finish();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here in the splash activity, it will check if there is a current user(if user is logged in) user.getUid() will return the current userid and then it will check if this userid is under the Drivers node, and then it directs him to the driver's home activity.
Your issue can be solved by the usage of SharedPreference mechanism.
Here you can keep the login status in SharedPreference like below:-.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.edit().putBoolean("login", true).commit();
when the user won't logged out and coming back, you should check the login status in splash screen using the SharedPreference variable and moved the user to corresponding activity.
Boolean loginStatus = prefs.getBoolean("login", false);
if (loginStatus) {
// Move to Home activity
} else {
//Move to login screen
}
Little bit documentation here
You might consider keeping a LauncherActivity in your android application which will decide which activity to launch further. A sample LauncherActivity might look like the following.
public class LauncherActivity extends AppCompatActivity {
private boolean loggedIn = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean isLoggedInAsDriver = getSharedPreferences.getBoolean("is_driver", false);
startApplication(isLoggedInAsDriver);
finish();
}
private void startApplication(boolean isLoggedInAsDriver) {
Intent intent;
if (isLoggedInAsDriver) {
intent = new Intent(LauncherActivity.this, DriverActivity.class);
startActivity(intent);
finish();
} else {
intent = new Intent(LauncherActivity.this, RiderActivity.class);
startActivity(intent);
finish();
}
}
}
Your application can have only one launcher, so mark the LauncherActivity as the launcher of your android application in the AndroidManifest.xml like the following.
<activity
android:name=".LauncherActivity"
android:label="Your app name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Now you may have noticed that I have used a flag (i.e. isLoggedInAsDriver) to start the desired activity from the LauncherActivity. This is a preference data that you need to store in your SharedPreferences when you can determine if the user is a driver or rider. Then each time your application launches via your LauncherActivity, check the stored value and get the user type. Based on the user type, launch the second activity.
use this code in manifest
and also
you should save user type with user ID in database and at start app check this first and then show home page.
<activity android:name=".MainActivity/>
<activity android:name=".DriverHomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

startActivity() doesn't start activity

I have a class called ShowBoardList where I check if user has logged in. If user hasn't logged in, then I want to return to the MainActivity which provides the user with buttons to login into different services.
My AndroidManifests.xml looks like this:
<application
<activity android:name="im.chaitanya.TaskTimer.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="im.chaitanya.TaskTimer.WebViewActivity" >
</activity>
<activity android:name="im.chaitanya.TaskTimer.ShowBoardList"
android:label="Your Tasks">
</activity>
</application>
ShowBoardList.java looks like this:
...
Intent mainActivityIntent = new Intent(ShowBoardList.this, im.chaitanya.TaskTimer.MainActivity.class);
Intent intent = getIntent();
String url = intent.getStringExtra(WebViewActivity.EXTRA_MESSAGE); //url can be null here
Keys keys = new Keys(); //this gets an API key
SharedPreferences settings = getSharedPreferences("mySettings", 0);
String savedToken = settings.getString("token", "Empty");
if (MyUtils.equalsWithNulls(url,"tasktimer://oauthresponse#token=")) {
Log.d("From ME:", "I've reached inside url check");
mainActivityIntent.putExtra(caller, "ShowBoardList");
//caller is a String. I'm storing the name of the current activity (ShowBoardList) in it.
//So that the main activity (which I'm trying to call) will know where the call came from.
startActivity(mainActivityIntent);
}
if(savedToken.equals("Empty") || savedToken.equals("")) {
String searchString = "#token=";
int tokenIndex = url.indexOf(searchString) + searchString.length(); //Since url can be null there can be an error here
String token = url.substring(tokenIndex);
savedToken = token;
SharedPreferences.Editor editor = settings.edit();
editor.putString("token", token);
editor.apply();
}
...
Condtion equalsWithNulls checks if url is null OR equal to the string in the argument. I have log statements there to check whether control reaches inside the if statement. The main activity however doesn't start.
Edit: onCreate() of MainActivity.java looks like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getSharedPreferences("mySettings", 0);
String token = settings.getString("token", "Empty");
Intent intent = new Intent(this, ShowBoardList.class);
if(token != "Empty") {
startActivity(intent);
}
intent = getIntent();
String callerActivity = intent.getStringExtra(ShowBoardList.caller);
View coordinatorLayoutView = findViewById(R.id.snackbarPosition);
if (callerActivity!=null && callerActivity == "ShowBoardList") {
Snackbar
.make(coordinatorLayoutView, "Permission Denied", Snackbar.LENGTH_LONG)
.show();
}
setContentView(R.layout.activity_main);
}
Try to define your new Intent wherever you required.
Intent newIntent = new Intent(ShowBoardList.this, im.chaitanya.TaskTimer.MainActivity.class);
newIntent .putExtra(caller, "ShowBoardList");
startActivity(newIntent );
My solution is based on Sourabh's comment on the question. I realised from my logs that the activity was indeed being started.
What I didn't realise was that when startActivity() is called, the calling activity (in this case ShowBoardList) is paused and when ShowBoardList was being called again, it would resume from after startActivity().
Therefore the solution here was to call finish() and then return immediately after the startActivity() which ensures that onCreate is called the next time. I hope that makes sense if anyone is in the same situation.
These questions helped me understand more about finish():
about finish() in android
onCreate flow continues after finish()

Killing the activity when user clicks the home button

Okey, this is weird.
I have two activities (well, more than two, but they don't matter) - AppActivity and PopupActivity. AppActivity is my main application activity and it contains my app's settings. PopupActivity is a dialog that gets opened when user clicks the button in the notification (it's a RemoteViews notification). Well, it works. Works great. But only if user closed the AppActivity by clicking the back button. If they clicked home, PopupActivity opens after a few seconds after clicking the button. Same thing happens when user closes the PopupActivity with a home button. Clicking the button in the notification doesn't open the PopupActivity instantly, but it takes a few seconds to kill the previous activity that still exists somewhere in the background.
I've tried calling finish() in the onStop and onPause methods, but it doesn't fix my problem.
Edit: Here's the code I have:
Manifest:
<activity
android:name="cc.lupine.quicksocial.AppActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize" android:noHistory="true" android:clearTaskOnLaunch="true" android:finishOnTaskLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:configChanges="orientation|screenSize"
android:excludeFromRecents="true"
android:showOnLockScreen="false"
android:theme="#android:style/Theme.Holo.Light.Dialog.NoActionBar.MinWidth"
android:name="cc.lupine.quicksocial.PopupActivity"
android:noHistory="true"
android:launchMode="singleInstance"
android:taskAffinity=""
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<service android:name="cc.lupine.quicksocial.ShareService"></service>
ShareService.java (just a function that gets called when user clicks a button in the notification):
public static void startSharing(Context ctx, int n) {
Log.d("sn", "startsharing called in shareservice");
if(n == 1 || n == 2)
{
Intent i = new Intent(ThisApplication.getAppContext(), PopupActivity.class);
i.putExtra("shareType", n);
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY|
Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_SINGLE_TOP|
Intent.FLAG_ACTIVITY_CLEAR_TASK|
Intent.FLAG_FROM_BACKGROUND|
Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(i);
} else if(n == 3) {
// doesn't matter for now
}
}
PopupActivity.java (fragments):
public class PopupActivity extends Activity implements OnDataPass {
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("sn", "oncreate called in popupactivity");
super.onCreate(savedInstanceState);
instanceOfPopupActivity = this;
setContentView(R.layout.activity_popup);
ShareFragment sfrag = new ShareFragment();
Bundle args = new Bundle();
Bundle extras = getIntent().getExtras();
try {
//doesn't matter
} catch(Exception e) { e.printStackTrace(); finish(); }
sfrag.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.contFragment, sfrag);
fragmentTransaction.commit();
}
#Override
public void onPause()
{
Log.d("sn", "onpause called in popupactivity");
finish();
super.onPause();
}
#Override
public void onStop() {
Log.d("sn", "onstop called in popupactivity");
super.onStop();
}
#Override
public void onDestroy()
{
Log.d("sn", "ondestroy called in popupactivity");
super.onDestroy();
}
}
And if I open the popup for the first time:
05-26 14:37:14.149: D/sn(7218): startsharing called in shareservice
05-26 14:37:14.179: D/sn(7218): oncreate called in popupactivity
But when I close the popup with a home button and try to open it again:
05-26 14:38:11.620: D/sn(7218): startsharing called in shareservice
05-26 14:38:14.103: D/sn(7218): oncreate called in popupactivity
It takes a lot of time for onCreate to be called. And now, what's the reason?
I think you are focusing on the wrong problem (killing the activity) instead of focusing on the real problem (10 seconds to start it again).
First you need to understand WHY it is taking 10 seconds to open it if you exited the other acitivty with the home key!
If you posted more details (with source code) it would have been easier to understand and help you!
call finish() when you want to close app. Call finish() on both activities and watch out popup window has to be focusable to handle click.

Android getapplication() data

I am using getHotelName() and setHotelName() to store data in the application and then access it. In my main activity that is categorized as Launcher both the methods and the getApplication() works in this activity. But when I try to access the getApplication from a different activity which is called from the main activity it gives a force close error.
This is my manifest file :
<application android:icon="#drawable/icon" android:label="#string/app_short_name" android:name="RestaurantNetwork" android:allowClearUserData="true" android:theme="#android:style/Theme.Black" >
<activity android:name="RestaurantActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="NetworkCommunication"></activity>
<uses-permission android:name="android.permission.INTERNET" />
</application>
In the main activity
RestaurantNetwork application = (RestaurantNetwork) getApplication();
application.setHotelName(this.hotelname.getText().toString());
Intent intent = new Intent(view.getContext(), NetworkCommunication.class);
startActivity(intent);
In the NetworkCommunication activity
public class NetworkCommunication extends Activity {
RestaurantNetwork application = (RestaurantNetwork) getApplication();
String hotelname = application.getHotelName().toString();
#Override
public void onCreate(Bundle savedInstanceState) {
I solved it using intent.extra, yes i had to get rid of the getApplication(). Would still like to know why there was an error in the prev method
in my main activity
this.hotelname = (EditText) findViewById(R.id.HotelName);
//RestaurantNetwork application = (RestaurantNetwork)getApplication();
//application.setHotelName(this.hotelname.getText().toString());
Intent intent = new Intent(view.getContext(), NetworkCommunication.class);
intent.putExtra("hotelName",this.hotelname.getText().toString());
startActivity(intent);
in my second activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchresults);
String value = null;
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
value = extras.getString("hotelName");
}
Toast.makeText(getApplicationContext(),value, Toast.LENGTH_SHORT).show();

Categories

Resources