Prevent back button after logout in android - java

After logout the user is directed to login screen in android. Now, if user clicks on back button of phone it should stay on login screen itself.
How can I make it possible in android?
I have used following code in my application but it will close my application. It should stay on the login screen only
Intent objsignOut = new Intent(getBaseContext(),Hello.class);
objsignOut.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objsignOut);
Please guide me the correct way.

override the onBackPressed in your login activity, to do nothing..
public void onBackPressed() {
//do nothing
}

It seems to me that there are simpler and cleaner solutions than overriding onBackPressed method, as mentioned here and here.
You can provide flags when launching a new activity (on login or logout) to simply clear the "back-stack" rather than override the behavior for the back-button:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
This is a safer solution that can also be used after you log-in and not just after you log-out.

public void onBackPressed(){
if(appCanClose){
finish();
}
}
These functions can exist in both the system framework (used if not in your code), as well as in your code. If you leave it empty, the app will do nothing when the back button gets pressed.
In this example, when the boolean value appCanClse is true, the back button will quit the app, if false, the back button wil do nothing. I would make sure the user still has someway to quit the app. :p

You can do it by just adding this two line of codes
#Override
public void onBackPressed(){
moveTaskToBack(true);
}
It will prevent going back to previous activity as well as take the app to background when anyone hits back button

The actual solution is
#Override
public void onBackPressed() {
super.onBackPressed();
finishAffinity();
}
add this code in Login Activity. App closes when back button clicked in login page.

Related

How do I overwrite back button of BSImagePicker gallery?

I'm new new in Java and working on an old project that uses bsImagePicker. There's a bug in my current project that when the user wants to click the below back button of the gallery it takes the user to home page.
This shouldn't be the default behavior, rather it should take the user 1 step back. Please, How do I overwrite the method of this back button? Unfortunately, I couldn't find any xml file that has referenced this button neither there's any previous overwritten method.
Thanks in advance.
Edit: I searched more and figured out maybe it has to do something with getSupportActionBar().setDisplayHomeAsUpEnabled() but I don't know how to set its android:parentActivityName attribute.
To override onBackPressed() method and implement your own functionality or behavior.
#Override
public void onBackPressed() {
//implement your own functionality in this
}
But since you are trying to implement it for back button shown in attached image and you don't have reference of it. Either you can create a reference by own or you can try on onCancelled method as provided in BSImagePicker gallary github repo.
//Optional
#Override
public void onCancelled(boolean isMultiSelecting, String tag) {
//Do whatever you want when user cancelled
}

Android Back Button deleting activity for good

I am making a game, and when a level is complete I have a pop-up activity which tells you your score.
When I press the back button I want this instance of the pop-up activity to be deleted for good.
Currently I get the behaviour;
Finish level, pop-up appears
I press back button to get rid of it
Start a new level, finish it and new pop-up appears
If I press the back button twice, the new pop-up disappears and the old one re-appears.
I want to delete the old pop-up as soon as it is hidden.
I am using the following code in my pop-up activity;
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
Any help would be much appreciated.
Thanks!
You can use this code. It clears off the PopupActivity form the Top Activity Stack so that it will not appear again.
#Override
public void onBackPressed() {
Intent removeIntent = new Intent(GameActivity.this, PopUpActivity.class);
removeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(removeIntent);
}

Android Application for Kids

I'm Developing Android Application for Parental Controlling, so I want to disable home and back button. Because kids are unpredictable, their can push home button or back button and so on. Than i want to disable sliding notification bar. (Because notification bar can change device configuration and setting)
I'm planning to create Android Application which is similar to the Samsung Kids Mode-Parental Control.
Is possible to create application like Samsung Kids Mode? Can you give some link/article or even example program to me?
I Have tried this :
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
but it not handle home button, the Home button still working.
Thanks in advance to everyone for taking time to read this, hoping a positive solution.
I dont know of anyway to override the home button. Im not sure that it can be done. You can override the back button like so.
#Override
public void onBackPressed() {
//do nothing, important dont call super.onBackPressed
}
Another this you can do is set the app to immersive mode to reduce the chance of accidently exiting the app
https://developer.android.com/training/system-ui/immersive.html
Extended from Activity
#Override
public void onBackPressed() {
}
can be used to override back button. Be sure to remove super.onBackPressed().

Pause Android app-flow

When a return button is pressed in the app I am working on:
public void clickReturn(View view)
{
// ...
// Call an event handler before finishing the activity
_appData.CustomDetail.hookBeforeFinish(this, new ArrayList<CompSubmission>(_submissions));
// Finish this activity
finish();
}
I am not able to modify clickReturn() - the way the application is structured, all I have access to is the hook hookBeforeFinish().
What I want to do is add a dialog to prompt for some input. I can do this in hookBeforeFinish(), but it only appears for a split second. I assume what is happening is I set up the AlertDialog builder, call builder.show() but finish() is being called directly afterwards - so the dialog only appears momentarily.
Is there anything I can put after builder.show() in the hook function such that it won't continue execution to finish()? If I can pause the application flow until someone presses the OK button on the dialog thatwould work better for me.
Thanks
Please comment finish().
Then read and add a dialog as explained here. In your positive button click (e.g. OK button), add finish();
Edit
Since you cannot override clickReturn() or change the class, you can use your hookBeforeFinish() to create a dialog and comment 'clickReturn();' line.
In your dialog, in the listener for the button which expects to finish the activity add clickReturn();.
This way you will pause the activity closing until user decides to do so.

Iconsistent results when returning to a previous activity

I have an android app that has various activities.
for example there is a home screen and an update screen.
The home screen is a list activity.
A button on the home screen brings you to the update screen that updates the database from a server. In theory when Returning to teh homescreen after an update, the list should be changed to reflect the update just done.
the code for going to the update screen is as follows:
Button synch = (Button) findViewById(R.id.synchButton);
synch.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
Intent intent = new Intent(HomeScreen.this, SynchScreen.class);
startActivity(intent);
}
});
and the code for returning back to the homescreen after the updates is:
main_menu.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
finish();
}
});
the list is compiled from an async task that runs in onStart, so my understanding is that onStart should run when I return to the homescreen, thus always displaying the most up to date version of the list.
On my Emulator I always get teh updated list, but when I run it on my phone the list is never updated, it just returns to the state of teh screen before I did the update.
any ideas?
thanks
Kevin
Check the Activity lifecycle section of the Android documentation. The code updating the view should probably be moved to onResume, since the Activity might not get killed when launching a new one.
Put the code for starting the Asynctask in onResume. Read the documentation related to activity life cycle.
#Override
public void onResume() {
super.onResume();
Apaptor.refreshListView(Vector);
}

Categories