Force splash to show from onCreate Activity - android

My app has the following Activities:
DashboardActivity
SplashActivity
MainActivity
This how the flow is required:
The dash board has a button that starts up the MainActivity. The onCreate() method of the main activity checks if a user-profile was previously created, if yes, then it starts normally (this is fast, no GUI delay).
If no user-profile is found, then it's required to display a splash screen having instructions/how-to for the mainActivity, meanwhile the onCreate() of the main activity creates a new user-profile file(slow and GUI blocking).
What I'm currently see is the splash/instructions showing delayed after the slow user-profile creation ends.
Here is a code snippet from MainActivity.
private void showUsage(){
Thread splashTread = new Thread() {
public void run() {
try {
Intent instructionIntent = new Intent(MainActivity.this,
InstructionsActivity.class);
startActivity(instructionIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
};
splashTread.start();
}
#Override //MainActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (!IsProfileExists()){
showUsage();
try{
createUserProfile(); //slow!
} catch (Exception e) { }
}
/*Continue with MainActivity*/
}
The Splash dismisses itself by a click:
public class InstructionsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.instructions_layout);
ImageView instructions = (ImageView) findViewById(R.id.ivInstructions);
instructions.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
The problem:
The Instructions Activity (splash) shows after the GUI gets block from the MainActivity onCreate().
Any clues?

Do the condition check (IsProfileExists()) from dashBoardActivity and call the Splash activity if profile doesn't exist.

Related

Show loading animation while Intent is loading

I am trying to create a splash screen that should show a rotating image while the next activity is being loaded.
Therefore I have two activities. The problem is that the rotation does not start, unless I add Thread.sleep before the Intent is created.
This is the first activity, where the animation should run while the second one loads.
import com.silencedut.taskscheduler.Task;
import com.silencedut.taskscheduler.TaskScheduler;
public class LaunchActivity extends AppCompatActivity {
private Task<Intent> loader;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
loader = new LoadingTask();
TaskScheduler.execute(loader);
}
#Override
protected void onDestroy() {
super.onDestroy();
TaskScheduler.cancelTask(loader);
}
private class LoadingTask extends Task<Intent>
{
LoadingTask() {
ImageView imageLoader = findViewById(R.id.imageViewLoader);
RotateAnimation rotate = new RotateAnimation(
0,360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF,0.5f);
rotate.setRepeatCount(Animation.INFINITE);
rotate.setRepeatMode(Animation.RESTART);
rotate.setDuration(5000);
rotate.setInterpolator(new LinearInterpolator());
imageNeedle.startAnimation(rotate);
}
#Override
public Intent doInBackground() {
DemoApplication app = (DemoApplication) getApplicationContext();
return new Intent(app, MainActivity.class);
return null;
}
#Override
public void onSuccess(Intent result) {
if (result != null) {
startActivity(result);
}
finish();
}
#Override
public void onFail(Throwable throwable) {
super.onFail(throwable);
}
#Override
public void onCancel() {
super.onCancel();
}
}
}
This class uses the TaskScheduler library, and the Task class is based on Runnable.
Inside the second class I add a Thread.Sleep to make the loading last more time.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try { Thread.sleep(5000); }
catch (InterruptedException ex) { android.util.Log.d("Exception", ex.toString()); }
...
}
...
}
My problem is that during these 5 seconds while MainActivity is loading, the animated image does not show or rotate. If I copy the same Thread.sleep block inside doInBackground (LoadingTask), then I can see the animation:
#Override
public Intent doInBackground() {
DemoApplication app = (DemoApplication) getApplicationContext();
// this makes the animation run
try { Thread.sleep(5000); }
catch (InterruptedException ex) { android.util.Log.d("Exception", ex.toString()); }
return new Intent(app, MainActivity.class);
return null;
}
So, how do I make it that the animation runs without using an unnecessary Thread.sleep inside doInBackground? I understand that if the second activity loads quickly, the animation won't show, but it doesn't work even when making it take more time.
You can do this Broadcast Receiver.
You don't need to use thread.
Simply start both the activity simultaneously.
In Splash Activity, register for broadcast reciver.and is Second activity send broadcast when all the data loaded. And then finish your splash activity.
If you didn't understand then I can give you an example.
Let me know.

Kill intent on splash screen

this is my Splash Screen, If I press home or multitasking/appswitch button when Intent is started app crash, in logcat is FATAL EXEPTION: Thread-1277. Can I kill/delete this Intent when player press home button?
public class SplashScreen extends Activity {
private static int loadingTime = 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.loading_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, loadingTime);
}
}
The following code tracks whether the SplashActivity is at least partially showing. If yes, it will continue to MainActivity. If not (activity is finished by pressing Back button, activity is stopped by pressing Home button) nothing happens.
This solution uses Fragments so the timing is preserved across e.g. screen orientation changes (it will always take specified time no matter how many times you rotate your device - the timer will not reset).
public class SplashActivity extends Activity {
// tracks when the activity is at least partially visible (e.g. under a dialog)
private boolean mStarted = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// your current code
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_startup);
if (savedInstanceState == null) {
// first time onCreate, create fragment which starts countdown
getFragmentManager()
.beginTransaction()
.add(SplashFinishFragment.newInstance(), SplashFinishFragment.TAG)
.commit();
} else {
// fragment already set up from first onCreate after screen rotation
}
}
#Override
protected void onStart() {
// the activity becomes at least partially visible
mStarted = true;
super.onStart();
}
#Override
protected void onStop() {
// the activity is no longer visible
mStarted = false;
super.onStop();
}
public boolean isStarted2() {
// there is already hidden method isStarted() in the framework
// you can't use it and are not allowed to override it
return mStarted;
}
public static class SplashFinishFragment extends Fragment {
private static final String TAG = SplashFinishFragment.class.getSimpleName();
private static final int DELAY = 1000; // one second delay
private static final Handler mHandler = new Handler(); // one main thread anyway
private final Runnable mRunnable = new Runnable() {
#Override
public void run() {
if (getActivity() == null) {
// this should never happen, there is no activity, so no fragment
Log.e(TAG, "No activity!");
return;
}
SplashActivity a = (SplashActivity) getActivity();
if (a.isStarted2() || a.isChangingConfigurations()) {
// if activity is even partially visible or is rotating screen right now, continue
Intent i = new Intent(a, SettingsActivity.class);
a.startActivity(i);
}
// in any case close splash
a.finish();
}
};
public static SplashFinishFragment newInstance() {
return new SplashFinishFragment();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// the countdown will continue (not reset) across screen rotations
setRetainInstance(true);
// try running the main activity after specified time
mHandler.postDelayed(mRunnable, DELAY);
}
#Override
public void onDestroy() {
// if the fragment gets destroyed (e.g. activity closes) do not launch main activity
mHandler.removeCallbacks(mRunnable);
super.onDestroy();
}
}
}
This was tested on a virtual Galaxy S2. It works when Home or Back button is pressed. It doesn't work when Recent Apps button is pressed. I don't know your use case but personally I would expect the app to continue launching while I browse recent apps.

Android - app always shows "Hello World" after splash activity

I'm new with Android.
I have an splash activity with a simple logo image and running this code on the .java class:
public class Splash extends Activity {
private int SPLASH_TIEMPO = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.anim_in_fade, R.anim.anim_out_fade);
}
}, SPLASH_TIEMPO);
}
}
Then, In my Login.class i don't have any code:
public class Login extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
}
In my login layout I made some designs. But when I run the application on my phone, It always shows the Hello world! text example:
What do you thing could be wrong?
Edit: In a debug mode, I see that the code to show the Login Activity is executing.

Reduce app CPU Usage in Android

My app shows a splash screen for 4 seconds and then opens another activity with a web view.
I am using a thread to splash the screen and finish() the first activity as soon as web view activity is started. I have also used animations like fade in and fade out.
It's CPU usage is fluctuating between 8% to 23%.What could be the reason. I want to reduce the CPU usage.
My first activity that shows splash screen and starts web view activity-
public class MainActivity extends Activity {
Thread splashThread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.animation.fadein,R.animation.fadeout);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
//-----------------------------------------
splashThread=new Thread(){
public void run()
{
try
{
sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(getApplicationContext(),OpenWeb.class);
startActivity(intent);
}
}
};
splashThread.start();
//-----------------------------------------
super.onStart();
overridePendingTransition(R.animation.fadein, R.animation.fadeout);
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
finish();
super.onRestart();
}
}
Could be that you used alot of imports something like. I heard that uses alot of cpu?
import android.graphics.*
Are you using a AVD?

Splash screen for Android tab bar application

I have created the android application with tabhost. In this application, I am having the 4 tabs and each one contains their separate webview. For this application, I want to add SplashScreen for the application before the tab bar's webview is loaded. How can I achieve this?
Create a different activity to show the splash which will be your launcher activity. After launching you can start the tabhost from this activity
Try this
public class SplashScreen extends Activity {
// time for splashscreen
protected int _splashTime = 5000;
private Thread splashTread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final SplashScreen sPlashScreen = this;
// thread for displaying the SplashScreen
splashTread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
// wait 5 sec
wait(_splashTime);
}
} catch (InterruptedException e) {
} finally {
// Go to Main activity
Intent i = new Intent();
i.setClass(sPlashScreen, MainActivity.class);
startActivity(i);
finish();
}
}
};
splashTread.start();
}
}
Hope it helps.

Categories

Resources