I need some help. I have a problem with my app. I want to put a splash screen at the beginning. I have done it before. I have made the code, the layout, and all works perfectly in a new project! When I put the code I run it on my phone and the layout in my application, the application runs perfectly without any errors. But when I open it on my phone, it stops and it doesn't open it!!! Can you suggest something??
my android manifest.xml:
android:name=".activities.SplashScreenActivity"
android:label="#string/splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Assuming you want to launch Main activity from your SplashScreenActivity.
In your SplashScreenActivity's onCreate():
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
finish();
The first 2 lines launch Main activity through an Intent, and the 3rd line kills SplashScreenActivity so you cannot go back to it from MainActivity.
public class SplashScreenActivity extends AppCompatActivity {
private int SLEEP_TIMER = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash_screen);
View imageView = findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade);
imageView.startAnimation(animation);
getSupportActionBar().hide();
LogoLauncher logoLauncher = new LogoLauncher();
logoLauncher.start();
}
private class LogoLauncher extends Thread {
public void run() {
try {
sleep(1000 * SLEEP_TIMER);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent(SplashScreenActivity.this,LoginActivity.class);
startActivity(intent);
SplashScreenActivity.this.finish();
}
}
Related
I'm trying to put a picture in my app but not really sure how.
Do I need a new activity for it? If so, how that activity will understand when to stop and go to the real program?
To clarify what I'm trying to do: When you open any app probably I think that they will have what I'm talking about. For example, the Youtube app when opened there will be a screen showing a the logo of Youtube before it opens the real app. I think this screen takes about 3 to 5 seconds.
Use splash activity as it was sais below. Here's example:
public class SplashActivity extends Activity {
private static String TAG = SplashActivity.class.getName();
private static long SLEEP_TIME = 2; // Time in seconds to show the picture
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar
setContentView(R.layout.splash_screen); //your layout with the picture
// Start timer and launch main activity
IntentLauncher launcher = new IntentLauncher();
launcher.start();
}
private class IntentLauncher extends Thread {
#Override
/**
* Sleep for some time and than start new activity.
*/
public void run() {
try {
// Sleeping
Thread.sleep(SLEEP_TIME*1000);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
// Start main activity
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
}
}}
And don't forget to add to the manifest
<activity
android:name=".SplashActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
It called Splash Screen
You should to create new activity which has a timer. After 1-2 seconds it launches your main activity.
How to implement Splash Screen in android
I have an activity that starts the system browser via Intent. Short before it does that I do a HTTP GET to some other URL. This GET will be answered as soon as the user finishes his task in the browser (logging in using OAuth).
I'd like to be able to close down the browser and / or get my application's activity back to the front.
I do not want to use a WebView because I'd like to avoid the perception that I might be trying to spy on passwords.
Any idea how to solve this? Is it possible at all?
Thanks a bunch!
Daniel
Make sure OAuth opens up URL which is something like yourapp://success
Next you add a intent filter to handle this custom protocol and address. More details are at http://developer.android.com/guide/topics/intents/intents-filters.html#ifs
Here's what did the trick in my project.
The application manifest is pretty standard:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Here's a code snippet of the background thread that sends the intent to re-show the activity:
public class AlarmThread extends Thread {
private int mSleepTime;
public AlarmThread(int sleepSeconds) {
super("AlarmThread");
mSleepTime = sleepSeconds * 1000;
}
#Override
public void run() {
Log.i("thread", "started sleeping for " + mSleepTime + " milliseconds");
try {
Thread.sleep(mSleepTime);
} catch (InterruptedException e) {
// ignored
}
Log.i("thread", "creating intent to bring activity to foreground");
Intent intent = new Intent(MainActivity.getContext(), MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.getContext().getApplicationContext().startActivity(intent);
}
}
Note that the trick is in the MainActivity.getContext().getApplicationContext().startActivity(intent); part (last line above).
In the MainActivity, I added the getContext method:
public static Context getContext() {
return mInstance;
}
And the member 'mInstance' is set in the 'onCreate':
private static MainActivity mInstance = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Other code....
mInstance = this;
}
I need to show one SecondActivity only once and only on first launch of the application. I implemented it like this (see below), but I don't really like a solution because I need to inflate layout on onResume() because if I do not I have an empty Activity when I click back hardware button being on SecondActivity.
public class TestActivity extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
public static final String FIRST_RUN = "FirstRun";
SharedPreferences sharedPreferences;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = getSharedPreferences(PREFS_NAME, 0);
if (sharedPreferences.getBoolean(FIRST_RUN, false)) {
setContentView(R.layout.main);
} else {
Intent i = new Intent(this, Second.class);
startActivity(i);
}
}
#Override
protected void onResume() {
super.onResume();
setContentView(R.layout.main);
}
}
In Second Activity I just put flag FirstRun to true.
In the first Activity call finish() after you make the call to startActivity(i)
Like this...
if (sharedPreferences.getBoolean(FIRST_RUN, false)) {
setContentView(R.layout.main);
} else {
Intent i = new Intent(this, Second.class);
startActivity(i);
finish();
}
You can then remove setContentView(...) from onResume().
The bestWay i can think is having Init activity that don't have any layout and just decides what activity to run first
If all you want is to prevent the user to go back to the activity, add the "noHistory" flag in your manifest file, like this:
<activity android:name=".SecondActivity" android:noHistory="true">
If this is your "splash screen" activity, and only needs to be shown on app start, do this:
<activity android:name=".SecondActivity" android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
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.
public class check extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
void myonclick(View view)
{
Intent mIntent = new Intent(this,check2.class);
startActivity(mIntent);
}
}
class check2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
Toast.makeText(
this,
"Welcome to second page", Toast.LENGTH_LONG).show();
finish();
}
}
Hi. This is my code when I run this. When I click a button it will show error in emulator: The Application Check has stopped unexpectedly.
Have you declared both the activities in manifest file ?
The main activity should have the following intent-filter tag
<intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
The second activity to be declared as
<activity android:name="check2">
Also calling finish() in the second activity would immediately return the control to the first activity.
In the Manifest File declare Two Activities like
<activity android:name=".LoginForm" android:label=" Login"/>
Here FrontPage is first file name
Here LoginForm is Second file name
Then When FrontPage file onclick the button the event will fire
code for that
Intent userintent = new Intent(FrontPage.this, LoginForm.class);
startActivity(userintent);
finish();