I'm working on a small app that has a Splash Screen and a User Login.
I want to launch different tab menu based on the user's ID.
Let say I have an Admin who has ID == "100" will get AdminTabActivity and rest of others will get UserTabActivity. so I wrote it like this. I'm simply passing the user ID from here to the MainActivity.
SplashActivity.java
public void run(){
try{
sleep(1000);
Intent in = new Intent(getApplicationContext(), MainActivity.class);
startActivity(in);
finish();
} catch (InterruptedException e){
e.printStackTrace();
}finally {
}
}
LoginActivity.java
//Stores and Grabs the user ID from sqlite db
db.addUser(json_user.getString(KEY_ID),
json_user.getString(KEY_NAME));
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra(KEY_ID, id);
startActivity(i);
finish();
MainActivity.java
Intent i = getIntent();
String id = i.getStringExtra(KEY_ID);
if("001".equals(id)){
/* Launch Admin Screen if ID == 100 */
Intent in = new Intent(getApplicationContext(), AdminTabActivity.class);
startActivity(in);
finish();
}else{
/* Launch User Screen if ID !== 100 */
Intent in = new Intent(getApplicationContext(), UserTabActivity.class);
startActivity(in);
finish();
}
If the user exits out of the app and returns back, it will skips the LoginActivity and go straight from Splash to MainActivity (which I want it to be), but the if block will skip this time even if the user has the ID == "100". Hope it makes sense, if not please kindly let me know so I can explain more. Thank you!
Related
I have developed an android apps that have a splash screen and a login page. The problem is when a user give credential and pressed log in button it open controlActivity. but when I pressed back button, I navigate to login page again If I pressed back button again I navigate to splash screen. How can I stop navigating to previous page, when user press back button then app should exit?
Why does it happen ?
This is happening because whenever you open another activity from intent, it creates it over it.
How to solve the problem?
You can add a flag like this
Intent intent = new Intent(this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
OR
You can finish it.
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
finish();
If you want to close the app onBackPressed, you can override the method. Add this to your activity.
#Override
public void onBackPressed(){
finish();
System.exit(0);
}
Doing this, the app will be closed on back pressed!
You need to put a flag in the activity you want to be forgotten.
Intent intent = new Intent(this, Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Answer by #Sambhav Khandelwal should solve your problem. In your splash screen you need to do add flags with intent like:
Intent intent = new Intent(SplashScreenActivity.this, LoginActivity.class); //assuming your splash screen name as SplashScreenActivity and login as LoginActivity
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Or you can do without flags like below:
Intent intent = new Intent(SplashScreenActivity.this, LoginActivity.class);
startActivity(intent);
finish();
I have a LoginActivity that is not tied to any online database, I only validate the username via edit text component to login and move to the MainActivity. LoginActivity appears when the value of text in MainActivity is null, therefore I created a validation method when the username is null, then the layout will move to LoginActivity, I made it like this with the aim that LoginActivity does not become the main layout of the app.
Validation users in MainActivity.java:
private void checkUsername() {
Intent intent = getIntent();
if(intent.getExtras() != null){
String users = intent.getStringExtra(LoginActivity.EXTRA_USER);
txuser.setText(users);
//NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String)
ContentValues cv = new ContentValues();
cv.put(Contract.UserEntry.NAME_COLUMN,users);
database.insert(Contract.UserEntry.TABLE_NAME,null,cv);
} else {
startActivity(new Intent(this, LoginActivity.class));
}
}
LoginActivity.java
private void loginUser() {
final String username;
username = user.getText().toString();
if (TextUtils.isEmpty(username)) {
Toast.makeText(this, "Name cannot be blank!", Toast.LENGTH_SHORT).show();
}
if (!cbAgree.isChecked()) {
Toast.makeText(this, "Login fails!", Toast.LENGTH_SHORT).show();
}
fabLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this, UtamaActivity.class);
intent.putExtra(EXTRA_USER, username);
startActivity(intent);
}
});
}
How do I fix this?, because when I open the app for the first time, the LoginActivity is successfully displayed, but when I enter to MainActivity it immediately forces close. I have included the logcat comment in the checkUsername() method
Change Intent intent = new Intent(LoginActivity.this, UtamaActivity.class);
to Intent intent = new Intent(LoginActivity.this, MainActivity.class);
I use this codes to send user to another applications. I wan't the user get send to playstore if the application not exist on he's phone. I was searching for examples but i didn't find anything.
// Launch My App one after clicking the button1
public void launchAppOne(View view) {
Intent launchAppOne= getPackageManager().getLaunchIntentForPackage("com.app.android.myapp1");
startActivity(launchAppOne);
}
// Launch My A after clicking the button2
public void launchAppTwo(View view) {
Intent launchAppTwo = getPackageManager().getLaunchIntentForPackage("com.app.android.myapp2");
startActivity(launchAppTwo);
}
You can use this code. It tries to launch the app and if it doesn't exist, the playstore page for the app is opened.
String packageName = "org.mozilla.firefox";
Intent intent= getPackageManager().getLaunchIntentForPackage(packageName);
if (intent != null){
startActivity(intent);
}else{
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName)));
}catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)));
}
}
I have some problems handling http response codes. The problem is that the app crashes because I do not give a specific function to run.
In this case I want to send the user back to login screen when the Httresponsecode is 401 otherwise the user can still use the app.
In my current code I have the following:
public boolean isUnauthorized(JSONObject response){
try {
if(response.getInt("StatusCode") == 401) {
return true;
}
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
What I want is here is to call so that when calling this function app wide it will do the same on every screen.
Intent i = new Intent(Register.class, Register.activity);
startactivity(i);
However this isn't possible because the ResponseHandler cannot extends Activity. If I do this I get a stack-trace error containing looper.prepare() must be called.
Can you anybody tell me how I can call a new intent from here. The class containing above is in a folder called components and my app activities are in another folder in case it is needed for giving the right answer.
Intent i = new Intent(Register.class, Register.activity);
startactivity(i);
This should be
Intent i = new Intent(CurrentClass.this, Activity.class);
startactivity(i);
if you have fragment
Intent i = new Intent(getActivity(), Activity.class);
getActivity().startactivity(i);
You need to pass the current Activity or the Application Context as argument for your Intent.
If it is inside a fragment then
getActivity().startActivity(getActivity(), newActivity.class);
If it is inside a class then:
context.startActivity(context, newActivity.class);
There are two flows in my app.
Flow 1:
Splash Screen --> Activity A --> Activity B --> Activity C --> Activity A
Flow 2:
Splash Screen --> Activity C --> Activity A
To explain the above; in Flow 1 a user sees the splash screen, goes to home screen, goes to login screen and on successful login, goes to main screen. If the user hits logout from the main screen he is redirected to the home screen.
In Flow 2 the user is an old user, he sees the splash screen and directly sees the main screen. If he logs out he should be directed to the home screen.
The problem which I am facing is that, in Flow 1, everything is working as expected. But in Flow 2, after logout (Activity C), the home screen (Activity A) is not opening. The intent is not working.
On click logout:
#Override
public void onClick(View v) {
((StudyStoryMain)getActivity()).logoutUser();
}
The Method:
public void logoutUser() {
//Problem: the intent is getting called in case the user creates an account. But, if the user is already an existing user, the intent is nit working
ParseUser.getCurrentUser();
ParseUser.logOut();
Intent i = new Intent(StudyStoryMain.this, HomeActivity.class);
//logic to fix logout
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(i);
}
Why is the Intent not working in both the flows?
Try this way :
Intent intent = new Intent(StudyStoryMain.this, HomeActivity.Class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity
Before finishing current Activity start another Activity :
startActivity(i);
finish();
As in logoutUser() method you are calling finish() before startActivity() which is wrong as startActivity(intent) get never call.
So just change sequence to
startActivity(intent)
finish()
#Override
public void onClick(View v) {
((StudyStoryMain)getActivity()).logoutUser();
finish();
}
The Method:
public void logoutUser() {
ParseUser.getCurrentUser();
ParseUser.logOut();
Intent i = new Intent(StudyStoryMain.this, HomeActivity.class);
startActivity(i);
}
Try this way, hope it solves your problem.