How can I avoid showing blank screen after user click Back button of device in the authentication step?
[ Edited]
The flow is user is asked to authenticate via twitter.
If the user reaches the step, it is possible that user doesn't authenticate and press the back button of device and return to the app. At that step, the app show blank page.
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (MyUtils.checkIsAuthenticated(prefs)) {
//sending the tweet
} else {
Intent i = new Intent(context,
GotoRequestTokenActivity.class);
context.startActivity(i);
}
}
});
Try overriding the onBackPressed() method ,you can do something there to disable the back button pr finishing your activity or display a relevant toast like "cancelling the authentication"
Your qsn is, from dialog, instead of pressing button on dialog user presses back key? or is it after you move to launched activity and user presses back?
in first case, i guess you shall check if user is bypassing dialog in OnResume.
in 2nd case, make your dialog an activity with dialog theme and call finish() in onclicklistener. (ur dialog shall dismiss when you go to next activity)
In any case, check ur log and see if you leaking something.
you use this demo for twitter integration because this demo opens a dialog for login instead of open the twitter url for login...so, if you want to back from that you can easily do that..see this link...This is just like facebook dialog..
https://github.com/lorensiuswlt/AndroidTwitter
Related
I am new to android programming. I am developing a simple app that provides users to book movie tickets. In my manifest file the start activity is title page. When we press enter button it shows login page. After login there will be a homepage which has some functionalities and logout button.
I used sharedpreferences to maintain session. But I got one problem.
After closing the application without logout and after opening the app again, it is showing the title page and after that login page.
But I need to show the homepage without going to the title and login page. How to do that?
I googled about this and tried so many examples. I couldn't find the answer.
Is this related to manifest start activity or what?
You could put logic that checks for the login status in the title activity, and there your application would decide which activity to load next(loginactivity if not logged in, homescreen if already logged in). Obviously you would also load the homescreen from the login activity after the user has logged in.
This is simple as this. Look at this example. The codes will speak out.
if (isLoggedIn()) {
//user is already logged in!
gotoHome();
} else {
//user is logged out!
goToTitlePage();
}
isLoggedIn function
public boolean isLoggedIn() {
Boolean mIsLoggedIn = getSharedPreferences("login_status_key",
MODE_PRIVATE).getBoolean("login_status_value"),
false);
return mIsLoggedIn;
}
Store the login status value when you are logged in successfully.
getSharedPreferences("login_status_key",
MODE_PRIVATE).edit().putBoolean("login_status_value"),
true).commit();
Edit the login status while you successfully logged out from your app.
getSharedPreferences("login_status_key",
MODE_PRIVATE).edit().putBoolean("login_status_value"),
false).commit();
Create A SplashScreen for your app and then write the following code:
SessionManager session=new SessionManager(Splash.this);
if(session.isUserLoggedIn()){
new Handler().post(new Runnable() {
#Override
public void run() {
boolean isloggedin=preftandp.getBoolean(Constants.LOGGED_IN false);
if(isloggedin)
{
startActivity(new Intent(LoginActivity.this,HomeActivity.class));
finish();
}else{
startActivity(new Intent(Splash.this,LoginActivity.class));
finish();
}
}
});
The logic is simple.Sitting on the splash screen you can decide which activity to launch based on Shared Preference values.
I need some direction on how best to handle a UI issue.
I'm working on an Android app that logins into a web application. I'm using webview to handle majority of the activities in the web applciation. I do have an override on the OnPageFinish method which goes back to my app, based on a url that the web application throws. The two instances this happens is 1.) credentials wrong, 2.) they logged out. Based on that URL match I start a new Intent within the OnPageFinish method. I also have an override on the OnPageStart to find #2 above which is working.
Issue: Because webview sees a specific url when incorrect credentials are used, the OnPageFinish method starts a new intent to go back to my login app. However I want to send up an alert for wrong credentials (#1 above) that the user acknowledges so they understand what the problem is.
When I throw the alert and the New Intent is inside the alert on the OnClick, the webview displays the web application's regular login screen behind the alert which is not visually appeasing. The alert does stay (desired behavior) until the person clicks ok on the alert. It will then go back to the app and shut down the webview. If I do the intent first outside the alert's OnCkick, then the alert, then I get the alert and it goes back to login application without the user seeing what the alert was for. It is very quick. Below is an example of what I did so far. I move the method setNewIntent to where I want it.
if (urlapp3.equals(url)) {
error = true;
setNewIntent(context);
new AlertDialog.Builder(MainActivity.this).setTitle("Incorrect Credentials").setMessage("Please re-enter your credentials correctly. They are case sensitive.").setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
}
The Intent Method
public void setNewIntent(Context context){
Intent intent;
intent = new Intent(context, MainActivity.class);
startActivity(intent);
}
I have almost completed my app development and I have even allowed a way to show up a toast message if there is no internet connection. I need to know how to close an app after showing a dialog if there is no internet connection?
My app is a webview app. So when user turn on the app, the app must check for internet connection. and display a dialog box if no network and the app must close after showing dialog.
OR
I don't want users to see the "webpage not available" page, so if there is no internet connection, the app must close after showing a dialog box with one button 'close'. Or is there any way to redirect users to a text in directory which shows no internet connection?
I need those code only; all the other codes have been done and are working properly.
Just invoke finish() to destroy activity in the onClick() function of dialog. Like:
...
builder.setMessage('There is no connection!! Please close the activity!')
.setPositiveButton('close', new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
...
You can popup a dialog after checking the internet connectivity as follows
new AlertDialog.Builder(this)
.setTitle("Connection Error")
.setMessage("You are not connected to Internet.")
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// finish the activity here or,
// redirect to another activity
}
})
.show();
You can check your internet connectivity before setting the view to the activity.
Like :
if(connnected){
setContentView(layoutId);
}else{
show a tosat with information no internet;
finish activity.
}
call finish() method on your activity once user clicks on dialogue
I'm developing an app that requires certain level of security. I need to have a feature when you press the home button and later enter in the app again, the app requires a password to continue where you've been staying before pushing the home button.
I already tried to do this, but when you call a new activity, the password screen is showed and I only need it when the home button is pressed:
#Override
public void onStop(){
super.onStop();
if (!isFinishing()) {
Intent i = new Intent(this, Register.class);
startActivity(i);
}
}
The normal flow is this:
Main->2nd Activity
but if I use the code that I showed, this is the flow:
Main->2nd Activity->Password Activity
It also works for home button but no for normal flow.
I already tried this and it works perfectly, but I don't want to add a new permission.
I am building an application which requires user authentication. First time a user opens the app should login or register to continue to the home screen of the app which loads some posts..
i should mention that the home screen should be a FragmentActivity to allow user navigate between 2-3 tabs.. this means that i should have another Activity (for the login screen or register) to allow the user to continue later to home.
MainActivity
|
|
--> Check If user logged in
| |
| |
| --> Start Login Activity (Or Register From Here)
|
--> Start Home Activity (FragmentActivity with 2-3 tabs-fragments)
Right now in the main Activity i am checking through shared preferences if user already logged in and then i start the FragmentActivity or the login Activity if user has not logged in.
I don't know if this is a problem, but when one of these two activities has started if i press back it goes on a blank screen and nothing happens. Seems logic because this is the MainActivity and its actually blank. i only have an if statement there to proceed to the appropriate activity.
Is this a nice approach or should i develop this with another way?
You can finish your main activity just after you navigate to home/login screen ex:
Intent intent=new Intent(this,Home.class);
startActivity(intent);
finish();
By doing this if user presses a back button on login or home page blank page wont be visible.
Also you can use your main activity as splash screen where you show some image and in background decide to go to login/home activity.
If you wish you put login screen as dialog box, which may help you. For creating login dialog you can use below code.
// Create Object of Dialog class
final Dialog login = new Dialog(this);
// Set GUI of login screen
login.setContentView(R.layout.login_dialog);
login.setTitle("Login to Pulse 7");
// Init button of login GUI
Button btnLogin = (Button) login.findViewById(R.id.btnLogin);
Button btnCancel = (Button) login.findViewById(R.id.btnCancel);
// Attached listener for login GUI button
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(Pulse7LoginDialogActivity.this,
"Login Sucessfull", Toast.LENGTH_LONG).show();
}
});
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
login.dismiss();
}
});
// Make dialog box visible.
login.show();
This code can be used...
Intent intent=new Intent(this,Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();