Splash screen only shows after clean install - android

I have made a splash screen with an Activity. I followed following example http://www.anddev.org/viewtopic.php?t=815 and it works perfectly when installed. But that's where my problem starts. It only shows after an install. So When I kill my app, the Activity isn't loaded anymore, it directly loads my second Activity. I think it's a little weird, because I set the intent-filter in my manifestfile on the splash screen.
Anyone knows how to solve this problem?
This is my manifestfile:
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:theme="#style/MyLightTheme.NoShadow" android:debuggable="true" android:name="android.app.Application" >
<uses-library android:name="com.google.android.maps" />
<activity android:name=".MainActivity" android:label="#string/app_name"></activity>
<activity android:name="FindJob" android:label="#string/FindJobTitel"></activity>
<activity android:name="JobDetails" android:label="#string/MaatTitel" ></activity>
.
.
.
<activity android:name="Splash" android:configChanges="orientation" android:label="#string/Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter></activity>
</application>
Splashscreencode:
private final int SPLASH_DISPLAY_LENGHT = 5000;
//private Drawable drawable;
private ProgressBar mProgress;
private int mProgressStatus = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splash);
mProgress = (ProgressBar) findViewById(R.id.progressBar1);
}
public void onStart(){
super.onStart();
//Log.i("START", "In on start");
Thread splashThread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while (mProgressStatus < 100) {
sleep(80);
//waited += 200;
mProgressStatus += 4;
mProgress.setProgress(mProgressStatus);
}
} catch (InterruptedException e) {
// do nothing
} finally {
finish();
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
}
}
};
splashThread.start();
}
UPDATE: placing android:noHistory="true" in the mainActivity in ManifestFile solves the problem. HOWEVER. Each time I leave the app, it restarts all of it (splash screen, mainactivity and not last activity loaded). But it's better then before.

Related

Intro screen is not opening

We decided to make an intro/welcome screen to our app. The activity called Welcome Activity needs to be launched when user goes to the application for the first time. All other times Main Activity needs to be launched. That's how I've done it in Android Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.google.android.gms.samples.vision.ocrreader"
android:installLocation="auto">
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:name=".OcrApplication"
android:allowBackup="true"
android:fullBackupContent="false"
android:hardwareAccelerated="true"
android:icon="#drawable/icon"
android:label="Ingredient analysis"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="ocr" />
<activity android:name=".WelcomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:windowSoftInputMode="stateHidden|adjustPan"
android:exported="true"
>
</activity>
<activity
android:name=".OcrCaptureActivity"
android:label="Read Text" />
<activity android:name=".ListResult" />
<activity android:name=".AllIngredients" />
<activity android:name=".IngredientDescription" />
<activity android:name=".Instruction" />
</application>
Is it a problem in my Manifest file or in the Welcome Activity code? I've used SharedPreferences in my onCreate. I have a class prefmanager
public class PrefManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
// shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "androidhive-welcome";
private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";
public PrefManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void setFirstTimeLaunch(boolean isFirstTime) {
editor.putBoolean(IS_FIRST_TIME_LAUNCH, isFirstTime);
editor.commit();
}
public boolean isFirstTimeLaunch() {
return pref.getBoolean(IS_FIRST_TIME_LAUNCH, true);
}
}
On create
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Checking for first time launch - before calling setContentView()
prefManager = new PrefManager(this);
if (!prefManager.isFirstTimeLaunch()) {
launchHomeScreen();
finish();
}
// Making notification bar transparent
if (Build.VERSION.SDK_INT >= 21) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
setContentView(R.layout.activity_welcome);
viewPager = (ViewPager) findViewById(R.id.view_pager);
dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
btnSkip = (Button) findViewById(R.id.btn_skip);
btnNext = (Button) findViewById(R.id.btn_next);
// layouts of all welcome sliders
// add few more layouts if you want
layouts = new int[]{
R.layout.welcome_slide1,
R.layout.welcome_slide2,
};
// adding bottom dots
addBottomDots(0);
// making notification bar transparent
changeStatusBarColor();
myViewPagerAdapter = new MyViewPagerAdapter();
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
btnSkip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
launchHomeScreen();
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// checking for last page
// if last page home screen will be launched
int current = getItem(+1);
if (current < layouts.length) {
// move to next screen
viewPager.setCurrentItem(current);
} else {
launchHomeScreen();
}
}
});
}
Try this, its working...
I have create Shared Preference in Main Activity and checked it in Welcome Activity.
If it run first time, then not found any Shared Preference and will Do Anything on Welcome Activity.
If it run second or more times, it found Shared Preference then redirect to Main Activity without launch Welcome Activity.
In Welcome Activity, onCreate method -
if ((getSharedPreferences("Demo", MODE_PRIVATE).contains("value"))){
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
finish();
}
else {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
finish();
}
},5000);
}
In Main Activity, onCreate method -
SharedPreferences.Editor editor = getSharedPreferences("Demo",MODE_PRIVATE).edit();
editor.putString("value", "1");
editor.apply();
Manifest file -
<activity android:name=".WelcomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
Have you ever call setFirstTimeLaunch(false) before? If not, that's because you return true as a default value in your function:
public boolean isFirstTimeLaunch() {
return pref.getBoolean(IS_FIRST_TIME_LAUNCH, true);
}
Try to change it to return pref.getBoolean(IS_FIRST_TIME_LAUNCH, false) and don't forget to setFirstTimeLaunch(true). My suggestion is to add it to this:
if (!prefManager.isFirstTimeLaunch()) {
prefManager.setFirstTimeLaunch(true); // Add this
launchHomeScreen();
finish();
}

Launch login activity instead of MainActivity if app is on its first run

I need my app to check if it's running for first time or not. If it's the first time, then it should launch LoginActivity instead of MainActivity. And if it's not the first run, it should display MainActivity as usual.
I used SharedPreference value to check if it's available, then app decides its not running it's first run.
This is what I've tried so far
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set default values into settings
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
// Check if the app is running on its first run
SharedPreferences fRun = getPreferences(MODE_PRIVATE);
if(fRun.getBoolean("firstrun", true)){
SharedPreferences.Editor editX=fRun.edit();
editX.putBoolean("firstrun", false);
editX.apply();
// Login activity stuff here
// Goto login screen
Intent loginIntent=new Intent(getApplicationContext(),LoginActivity.class);
startActivity(loginIntent);
//finish();
} else {
setContentView(R.layout.activity_main);
}
}
}
My problem is, when I run my app, it suddenly crashes and displays message Unfortunately, the app has stopped.
Why does the app crash? Is it because code in my LoginActivity have errors or do I need to first load MainActivity then call LoginActivity?
You can use LoginActivity as LAUNCHER activty and check whether the user is logged in. If yes, start MainActivity.
The AndroidManifest.xml:
<activity
android:name=".LoginActivity"
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=".MainActivity"/>
And the LoginActivity:
public class LoginActivity extends ActionBarActivity {
private static final String LOGIN_KEY = "LOGIN_KEY";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
if (pref.getBoolean(LOGIN_KEY, false)) {
//has login
startActivity(new Intent(this, MainActivity.class));
//must finish this activity (the login activity will not be shown when click back in main activity)
finish();
}
else {
// Mark login
pref.edit().putBoolean(LOGIN_KEY, true).apply();
// Do something
}
}
}
The MainActivity:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Do something
}
}
<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"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".Activity.MainActivity" />
<activity android:name=".Activity.SignupActivity" />
<activity android:name=".Activity.SigninActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
You need to rearrange your Activity classes a bit I think. It's very simple to decide if your application has run first time or not and launch some Activity based on this decision. I would like to suggest the following architecture.
You can set a LauncherActivity to decide whether you need to start LoginActivity or MainActivity like this:
public class LauncherActivity extends Activity {
private boolean firstLaunch = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i;
SharedPreferences pref = getSharedPreferences(Constants.ApplicationTag, MODE_PRIVATE);
firstLaunch = pref.getBoolean(Constants.FIRST_LAUNCH, true);
if (firstLaunch) {
i = new Intent(LauncherActivity.this, LoginActivity.class);
startActivity(i);
} else {
i = new Intent(LauncherActivity.this, MainActivity.class);
startActivity(i);
}
finish();
}
}
You have another problem I need to sort out is calling setContentView inside an else statement which is erroneous. You need to put setContentView just after the super.onCreate(savedInstanceState); in any of your Activity.
When you're putting it inside an else statement, the content view may not be set which will cause an application crash.
So remove the checking for first run from MainActivity and move that portion to LauncherActivity which will solve the problem.
The AndroidManifest.xml of the LauncherActivity may look like this
<activity
android:name=".Activities.LauncherActivity"
android:label="#string/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>

Splash Screen Hanging After System Stops App

Manifest File is
android:icon="#drawable/app_logo"
android:label="#string/app_name" android:debuggable="false">
<activity
android:name=".SplashActivity"
android:label="#string/title_activity_main"
android:launchMode="standard"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Splash screen code is
public class SplashActivity extends MusicActivity {
private final int SPLASH_DISPLAY_LENGTH = 7000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(SplashActivity.this,MenuActivity.class);
startActivity(mainIntent);
}
}, SPLASH_DISPLAY_LENGTH);
}
When you leave the app by using the 'home' key, I fire this from the activity that is running, (it's an audio player and this is the primary activity that you can exit from)
#Override
protected void onPause() {
super.onPause();
if (mp != null){
mp.pause();
if (isFinishing()){
mp.pause();
}
}
}
When I re-enter the app is just a few minutes things are perfect, the audio resumes where it was and so on -
However - if I walk away from the device for say an hour or so and then return and launch the app from the launcher, it freezes on the 'splash screen', no error message,no force close,it just sits there.
What should I be checking for?
Thanks

Intent in Timer Task causes force close

So I'm writing an app which, when plugged in, is supposed to launch an activity telling the user to unplug the device from a computer. In this class, I have a timer which periodically checks to see if it is plugged in. The code in the timer works other than when I try to start another activity when the user disconnects the cord. I am not sure if the issue lies in the class with the timer, or the Manifest, although the activity which I am trying to launch when disconnect was working earlier and I haven't made any changes to it.
Here is the code for the StateCheck class (tests whether its plugged in by checking the state of the SD card):
public class StateCheck extends Activity {
TextView stateCheck, unmountedImage, tester;
Timer timer;
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.state_check);
stateCheck = (TextView) findViewById(R.id.tvStateCheck);
unmountedImage = (TextView) findViewById(R.id.tvUnmountedImage);
tester = (TextView) findViewById(R.id.tvTester);
//Intent openStartingPoint = new Intent("com.flannigan.MUSERCISEACTIVITY");
//startActivity(openStartingPoint);
MyTimerTask myTask = new MyTimerTask();
timer = new Timer();
timer.schedule(myTask, 1000);
}
class MyTimerTask extends TimerTask {
public void run() {
String state = Environment.getExternalStorageState();
i++;
if (Environment.MEDIA_MOUNTED.equals(state)) {
unmountedImage.getHandler().post(new Runnable() {
public void run() {
unmountedImage.setVisibility(View.INVISIBLE);
}
});
stateCheck.getHandler().post(new Runnable() {
public void run() {
stateCheck.setText("SD card is mounted!\nLoading...");
}
});
Intent openStartingPoint = new Intent("com.flannigan.MUSERCISEACTIVITY");
startActivity(openStartingPoint);
} else {
MyTimerTask myTask = new MyTimerTask();
timer = new Timer();
timer.schedule(myTask, 1000);
tester.getHandler().post(new Runnable() {
public void run() {
tester.setText(""+i);
}
});
}
}
}
}
Manifest:
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".Splash"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StateCheck"
android:label="Musercise"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="com.flannigan.STATECHECK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MuserciseActivity"
android:label="Musercise"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="com.flannigan.MUSERCISEACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Use runOnUiThread for Accessing UI elements from TimerTask Thread as:
StateCheck.this.runOnUiThread(new Runnable() {
public void run(){
// start next Activity here
// Access UI element here
}
});
and for Starting Activity Change your Intent as:
Intent intent=new (StateCheck.this,MuserciseActivity.class);
startActivity(intent);

I am having issue with my splash screen as am not getting into the next activity

This is an example of splash screen but facing difficulty with it as I couldn't view my MainActivity class, couldn't be able to recognize the issue.Tried in manifest file by changing the action and the category name as well but could not be able to resolve to it.
Basically changed my Intent intent = new Intent(); as well but still the same goes on.
public class SplashActivity extends Activity
{
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
player = MediaPlayer.create(SplashActivity.this, R.raw.splash);
player.start();
Thread timer = new Thread()
{
#Override
public void run()
{
try
{
sleep(4000);
}catch(InterruptedException e)
{
e.printStackTrace();
}
finish();
Intent intent = new Intent();
intent.setClass(SplashActivity.this, MainActivity.class);
startActivity(intent);
stop();
}
};
timer.start();
}
#Override
protected void onPause()
{
super.onPause();
player.release();
finish();
}
}
====>And here is my manifest file --
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mainsplashcreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/android_splash"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SplashActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I would guess, that your problem is, that you finish your activity before you start the new one. If you call finish() on your activity, it will be destroyed, and so the startActivity() won't be called anymore (or if it will be called, it won't get a valid context anymore). So try to move the finish() method, at the end of your run method, that should solve the problem.
Another Very Short,Simple and Effective way of Implementing Flash Screen is as below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler=new Handler();
Runnable gotoMain=new Runnable() {
public void run()
{
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
};
handler.postDelayed(gotoMain, 4000);
}
You just Replace you OnCreate Method with this one.
There are a few things which you should take care of:
1. The usage of stop() and the place it is being used are not encouraged. Besides being deprecated, it will also give you UnsupportedOperationExcecption.
2. Is your MediaPlayer being initialized correctly without any errors? Check the logs.
3. What is the reason you are using finish() in onPause() method? It is not recommended.
4. You are assigning MAIN action to both your activities. While it is allowed, it should be for a specific reason.
However, all these things should not avoid your application to go to the main activity. There may be the reasons with your main activity code.

Categories

Resources