Reputation: 341
I am working on an android application. I have just started coding it. I want to have an initial activity which would check if a user is already logged in. If yes, then it will redirect it to the user's home page, otherwise it'll redirect the user to a login/signup page.
For now, I have created a simple main activity, which should show a progress bar. I've added a 5sec delay and then this activity should direct the user to a login page (I've not kept any button on this page). This activity creates an intent and start the other activity using this after the 5 sec delay. I'll store the user's login state in shared preference later.
The problem is that when I run the app on my device, it just shows a blank screen (not the progress bar that I had put) for 5 sec and then redirects to the login page. What I'm doing wrong?
Here's my activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:id="@id/firstProgress"
style="@android:style/Widget.ProgressBar"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp">
</ProgressBar>
</LinearLayout>
and here's my MainActivity.java file
package com.example.neeraj.myapplication;
import android.app.Activity;
import android.app.Notification;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookActivity;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SystemClock.sleep(5000);
Intent i = new Intent(MainActivity.this, LoginActivity.class);
startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
Upvotes: 0
Views: 769
Reputation: 12929
By using Thread.sleep() or SystemClock.sleep() you are blocking the main thread of the app, which is responsible for almost everything like displaying your UI. Use Handler.postDelayed() instead.
Upvotes: 2