Having issues trying to order the some of my activities in my application. Have implemented some ideas that I have seen on SO, but to no avail.
At the minute, my application is going SplashScreen > MainActivty. I want SplashScreen > LoginActivity > MainActivity
Any indications on where I'm going wrong would be appreciated.
Manifest
<activity
android:name="com.example.XXX.myapplication.SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.XXX.myapplication.LoginActivity"
android:parentActivityName=".SplashScreen">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.XXX.myapplication.SplashScreen" />
</activity>
<activity
android:name=".SignUpActivity" />
<activity
android:name="com.example.XXX.myapplication.MainActivity"
android:parentActivityName=".LoginActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.XXX.myapplication.LoginActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar" />
</activity>
You don't order activities in the manifest like this. Set your splash screen to the default activity. In the Java code for splash activity start mainactivity with the startactivity method. Then in loginactivity call startactivity for mainactivity.
https://developer.android.com/training/basics/firstapp/starting-activity.html
You should do it programmatically in Java, in your SplashScreen Class, you should have something like :
startActivity(new Intent(SplashScreen.this, LoginActivity.class));
Example :
private final int SPLASH_DISPLAY_TIME = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent myIntent = new Intent(SplashScreen.this, LoginActivity.class);
startActivity(myIntent);
finish();
}
},SPLASH_DISPLAY_TIME);
}
Place and `Intent-filter` tag in your intended entry activity in AndroidManifest.xml file like this
```
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
```
If your initial activity is a place, start the next activity in your runnable thread as shown below.
```
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent(SplashActivity.this,ACTIVITY_TO_GO_TO.class);
startActivity(mainIntent);
finish();
}
}, TIME_OUT_NOW);
```
You can then move to anywhere else in ACTIVITY_TO_GO_TO activity.
You can not ordering Activities by Manifest
1.Your Launch page is Splash.So you can write below code in splash page
Thread timerThread = new Thread(){
public void run(){
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
startActivity(new Intent(SplashScreenActivity.this,YourActivity.class));
finish();
}
}
};
timerThread.start();
You can write your Login Activity on plcae of YourActivity.
2.On Click Login button Ho to main Activity
Related
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>
For now, the InitialActivity is set as the main activity in the AndroidManifest.xml file.
<activity
android:name=".ui.SplashActivity"
android:noHistory="true" >
</activity>
<activity
android:name=".ui.InitialActivity"
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=".ui.LoginActivity"
android:noHistory="true" >
</activity>
<activity
android:name=".ui.RegisterActivity"
android:noHistory="true" >
</activity>
<activity
android:name=".ui.MainScreenActivity"
android:launchMode="singleTop">
</activity>
After finishing the registration process in the RegisterActivity, quit the application and re-start the app, I want the app to directly go to MainScreenActivity automatically logged in. So here's the part of the InitialActivity class that checks if I am logged in and goes to the MainScreenActivity if I am.
session = new SessionManager(getApplicationContext());
// check if user is already logged in.
if(session.isLoggedIn()) {
// User is already logged in. Take him to MainScreenActivity.
Intent intent = new Intent(InitialActivity.this, MainScreenActivity.class);
startActivity(intent);
finish();
} else {
startActivity(new Intent(this, SplashActivity.class));
}
So far, I managed to go directly into the MainScreenActivity with the code above, but in this case I miss the splash. How can I modify the code so that I can still see the SplashActivity even after being automatically logged in?
FYI here's the SplashActivity class code.
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler hd = new Handler();
hd.postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
}, 1500);
}
}
I will use the splashActivity as the launcher activity (it will be launch every time) and I will check in this activity if user is logged or not, depending on the response launch the RegisterActivity or the mainActivity
I'm new in this world. I have a problem when I use startActivity(intent).
This is the Manifest:
<activity
android:name="com.example.counter.Splash"
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="com.example.counter.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
And this is the code:
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run()
{
try
{
sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent i=new Intent ("com.example.counter.MainActivity");
startActivity(i);
}
}
};
timer.start();
}
I'd want to show Splash activity for 5 seconds and then show MainActivity.
LogErrors: !https://www.dropbox.com/s/kg7xyp6h4b95itq/Screenshot%202014-02-08%2016.57.36.png
There are two ways of doing what you are trying to do.
Using an implicit Intent
Using an explicit Intent
Refer Intent Types
Implicit Intent
Declare Intent Filters for your Activity in your AndroidManifest.xml. By doing that the Android system understands what kind of Intents your component(in this case your MainActivity) can handle.
<activity
android:name="com.example.counter.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.counter.MainAction" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<activity>
Now you will be able to launch your Activity with the same Intent
Intent i=new Intent ("com.example.counter.MainAction");
startActivity(i);
Such implicit Intents are used when you don't explicitly know which Activity has to be started and you want the Android system to decide which component to start. If the system finds multiple components which can handle your Intent, it will allow the user to choose.
Note: it is possible that there are no applications that can handle your intent. In this case, your application will crash when you invoke startActivity(). To avoid this, before calling startActivity() you should first verify that there is at least one application registered in the system that can handle the intent. To do this use resolveActivity() on your intent object.
Explicit Intent
In your case, you should use an explicit Intent as you already know which Activity you want to start. So create an Intent by passing the context and the component(Activity) class you want to start.
Intent i=new Intent (this,MainActivity.class);
startActivity(i);
You have to reference the class you want to start. So you'd need something like:
Intent newAct = new Intent(this, Splash.class);
startActivity(newAct);
What you're passing is an Action that is not understood as a class name.
I guess, Splash is your Launcher Activity, make following changes in your manifest file:
<activity
android:name="com.example.counter.Splash"
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="com.example.counter.MainActivity"
android:label="#string/app_name" >
</activity>
Make your activity this way:
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
/*Splach screen that display for 5 seconds when app start*/
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
finish();
}
}, 5000);
}
}
I hope this should solve your problem now.
I think you should be able to use (implicit Intent):
Intent i=new Intent ("com.example.counter.MainActivity");
There is no reason to change it to (explicit intent):
startActivity(new Intent(mContext, MainActivity.class));
but then you need to change the action in intent filterof MainActivity from:
<action android:name="android.intent.action.MAIN" />
to:
<action android:name="com.example.counter.MainActivity"/>
You need to declare an activity in manifest file.
Like this:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".FirstActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activty
android:name="com.example.counter.MainActivity"/>
</application>
Hope it helps.
i think it is better if you use Handler put this code at the splash Activity at the onCreate
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
}, 1500);
and if you want to open it one time it is good to use SharedPreferences
i'm trying to develope an app for a tablet (ICS 4.0.3) that will be used in public places like bar, resturant ecc..
The user that uses that tablet ( so my application ) could not go in home and only administrator, setting a code, can go out.
What i've done is:
public class MainActivity extends Activity {
private Activity actual;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (getIntent().getBooleanExtra("EXIT", false)) {
finish(); // it doesn't work
}
}
#Override
public void onBackPressed() {
// do nothing!
}
}
MANIFEST:
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
</intent-filter>
</activity>
ADMINISTRATION DIALOG:
TEST1: // application is relaunched
Intent homeIntent= new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(homeIntent);
TEST2: // application is relaunched
System.exit(0);
TEST3: // it open settings (o.O)
Intent h = new Intent(Intent.ACTION_MAIN);
h.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
h.putExtra("EXIT", true);
context.startActivity(h);
My problem is that i can't go out from application.. how can i solve?
In TEST3 you've only provided ACTION_MAIN in the Intent. Android looks for apps that can handle this action and finds a long list of them. How should it know that it should launch yours?
I assume you've set your app as a HOME-screen replacement. Try adding this to the code for TEST3:
h.addCategory(Intent.CATEGORY_HOME);
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.