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
Related
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
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>
Please help with an issue I'm facing.
First, I know, normally, when we open an app, navigate to some activity, click Home Button, and then return back to the app, we'll return to the last activity with all content saved.
But it's a little bit different in my app. There are 4 activities in my app. 'LoginActivity' is the main activity. When we click app icon, it will be triggered. And it will automatically start 'MainActivity' if user saved his/her login (using SharedPreferences). User can also launch the app by tapping the phone to NFC tag and 'NFCTagTriggeredActivity' will be triggered.
Now the issue is, if I launch the app by tapping the phone to an NFC tag, i.e. through 'NFCTagTriggeredActivity', click Home Button then go back to the app again, it cannot go back to 'NFCTagTriggeredActivity' anymore. Instead, it will go back to 'MainActivity'. Similarly, if I launch the app by tapping an NFC tag and navigate to 'OtherActivity', click Home Button then go back. It will go back to 'MainActivity'
I suspect this has something to do with intent-filter setting, but cannot figure out how to solve this issue. Attached the AndroidManifest.xml and part of Java code. Please let me know if any further info needed.
Any help is appreciated!
<activity
android:name=".LoginActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".NFCTagTriggeredActivity"
android:label="#string/title_activity_nfc_tag_triggered"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="test/nfc"/>
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".OtherActivity"
android:label="#string/title_activity_other"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
</activity>
LoginActivity
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
if (sharedPrefs.getInt(Configs.USER_ID, 0) != 0 && sharedPrefs.getBoolean(Configs.LOGIN_STATUS, false)) {
Intent intent = new Intent();
intent.setClassName("com.myapp",
"com.myapp.MainActivity");
this.startActivity(intent);
finish();
}
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
NFCTagTriggeredActivity
public class NFCTagTriggeredActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(NFCTagTriggeredActivity.this);
if (sharedPrefs.getInt(Configs.USER_ID, 0) == 0 || !sharedPrefs.getBoolean(Configs.LOGIN_STATUS, false)) {
Toast.makeText(NFCTagTriggeredActivity.this, "Please log in or sign up first.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClassName("com.myapp",
"com.myapp.LoginActivity");
this.startActivity(intent);
finish();
}
setContentView(R.layout.activity_nfc_tag_triggered);
Intent mIntent = this.getIntent();
Parcelable[] parcelables = mIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
tag = this.getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
//...
}
}
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);
Im writing a program that offers a quick reply dialog upon receipt of an SMS.
However, I am getting an unexpected result. When I receieve an SMS, the appropriate dialog activity comes up displaying the correct phone number and message, however there is a second activity behind it that is the 'default' activity in my program (it is what opens when i launch my application)
I do not want this second activity to come up. The quick reply activity should come up by itself over top of whatever the user was doing before.
The 'floating' activity:
public class quickReply extends Activity {
String mNumber, mMessage;
TextView mMainText;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMainText = (TextView)findViewById(R.id.mainText);
try{
Intent i = getIntent();
Bundle extras = i.getExtras();
mNumber = extras.getString("theNumber");
mMessage = extras.getString("theMessage");
this.setTitle("Message From:" + mNumber);
mMainText.setText(mMessage);
} catch(Exception e) {
mMainText.setText(e.getMessage());
}
}
}
The call to the activity inside an onReceive()
Intent i = new Intent(context, quickReply.class);
i.putExtra("theNumber", mNumber);
i.putExtra("theMessage", mMessage);
i.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
The Manifest:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".quickReply"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
the only way I have found that works, in your activity definition in manifest:
android:launchMode="singleInstance"
but then you have to relaunch your main/default activity once the dialog is dismissed. NOTE: you will lose all state from the previous launch, so this is a less than ideal solution.
UPDATE:
you can also do this by:
Intent.FLAG_ACTIVITY_CLEAR_TASK
so here's what I did:
open the original/main activity
from a service, launch the dialog style activity using the above (main goes bye-bye).
when the user dismisses the dialog, start main again with an extra intent (IS_BACK) that is processed in onCreate() and calls:
moveTaskToBack(true);
this will keep the task under the dialog on top and your main in the back of the stack.
You should set the task affinity of the activity to something different than your main activity. This will separate it from the main activity and it will track as a separate task:
<activity android:name=".quickReply"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog"
android:launchMode="singleTask"
android:taskAffinity="quickReply"
>