BottomSheetBehavior is collsaping but backpress is not working why? - java

I am trying to collapse bottomSheetBehaviour, bottomSheetVehaviour collapse perfect but after that when I backPress, backpress dosen't work remains in the same activity. Can anyone please give some solution.
#Override
public void onBackPressed() {
if (!mbottomSheetBehavior.isHideable()){
super.onBackPressed();
}else {
mbottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}

Related

Android - On back press

I am writing a code for onBackPressed() method.
Actually, I want that if the user click on back press it check that If the RecyclerView is visible then it should just close the RecyclerView or if it is invisible it should go to another activity.
How I can do it ? Thanks
Simple you can check the visibilty of the recyclerView like
if ( recyclerView.getVisibility() == View.VISIBLE )
and can can decision what you want to do.
For Example: You have to #Override onBackPressed() function to manage your logic like this:
#Override
public void onBackPressed() {
if (recyclerView.getVisibility() == View.VISIBLE) {
//do it here when recyclerview in visible
} else {
//go to any activity or
//simply supper method will take you on previous activity
super.onBackPressed();
}
}
Assume you have done something like recyclerView = findViewById(R.id.something) already.
#Override
public void onBackPressed() {
if (recyclerView.getVisibility() == View.VISIBLE) {
recyclerView.setVisibility(View.INVISIBLE);
} else {
super.onBackPressed();
}
}
As suggested by #bruno above, actually you have the answer in mind already. All you have to do is implement it and see if it works.

Back when back button is pressed in webview

I have an Android app developed in Android Studio that works in webview. I want to go to the last file when back button is pressed from the phone's navigation bar. Here is what I have tried-
private WebView myWebView;
#Override
public void onBackPressed(){
if (myWebView.canGoBack()){
myWebView.goBack();
} else {
super.onBackPressed();
}
}
But my app crashes when I press back. What I am doing wrong?
This is working for me.
#Override
public void onBackPressed() {
WebView webView = findViewById(R.id.webView);
if (webView.canGoBack()) {
webView.goBack();
}
else {
......
}
}

How to Implement onBackPressed for Android Fragments(Tabs Activity)

I am developing an Android Application which contains the Tabs Activity. My Tabs activity contains four tabs(Fragments--[1][2][3][4]). What I want is when I press the Back button it must be redirected to the previous tab, not to the first tab. Like
[4] -> [3]
[3] -> [2]
[2] -> [1]
[1] -> Alert to logout from the App
Please help me out. What do I have to write in my TabsActivity class.
Assumption
Your four fragments is hoted with ViewPager.
Logic
You can write your code into
#Override
void onBackPressed()
{
if(viewPager.getCurrentItem==3)
{
viewPager.setCurrentItem(2)
}
else if{
}
}
and like wise
Use logics instead of hardcoding page numbers.
Below solution works with 2,3,... n, every number of items in Viewpager.
#Override
public void onBackPressed() {
if (viewPager.getCurrentItem() != 0) {
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
} else super.onBackPressed();
}
If you are using TabHost then you can use this solution.
In your attached Activity onBackPressed() you have to implement like below,
#Override
void onBackPressed()
{
if(viewPager.getCurrentItem==3){
viewPager.setCurrentItem(2)
}
else if{
//do your stuff here
}
}

Android onBackPressed() is not being called?

in my MainActivity, which extends from AppCompatActivity, I want to override the onBackPressed method like so:
#Override
public void onBackPressed() {
Log.d("MainActivity","onBackPressed");
Toast.makeText(getApplicationContext(),"onBackPressed",Toast.LENGTH_SHORT).show();
}
but onBackPressed does not get called. How ever if I do not override onBackPressed, the application closes, when I press the backbutton and if I do override it it doesn't.
The rest of my activity looks like this:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private Drawer drawer;
private FloatingActionButton fab_test;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab_test = (FloatingActionButton) findViewById(R.id.fab_test);
fab_test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"FAB Test pressed",Toast.LENGTH_SHORT).show();
}
});
buildDrawer();
getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer,page).commit();
}
#Override
public void onBackPressed() {
Log.d("MainActivity","onBackPressed");
Toast.makeText(getApplicationContext(),"onBackPressed",Toast.LENGTH_SHORT).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
}
EDIT: I'm talking about the hardware-backbutton(not the actionbar one)
This question is already answered, but I feel to clear something here in this topic. Most comments and answeres point out to use super.onBackPressed() and that this is the cause of the not working method onBackPressed(). But that is not correct and important to let other beginners know. The method onBackPressed() does not need to use super.onBackPressed() . onBackPressed()also works if somebody, for example, comment super.onBackPressed() out.
As the questionier has written, he won´t use super.onBackPressed() because it will close the activity. So, the cause of this why it isn´t working, could be seperated into three possible causes:
The Log doesn´t work because of a wrong filter in the logcat console
The Toast dosn´t work because of the wrong passed context
The OS is implemented wrong by the supplier.
Usually, the toast works by passing the correct context. In the case of questioner, simply passing this .
#Override
public void onBackPressed() {
Log.d("MainActivity","onBackPressed");
Toast.makeText(this,"onBackPressed",Toast.LENGTH_SHORT).show();
}
For the Log, simply set the correct filter on logcat.
I don´t care if somebody give downvotes now, but it must be clear for other beginners, that super.onBackPressed() must not be used.
Anyway, the use of onKeyDown() also is a solution.
The onBackPressed() is a default action called from onKeyDown() in API < 5 and a default action called from onKeyUp() from API level 5 and up. If onKeyUp() does not call super.onKeyUp(), onBackPressed() will not be called.
Documentation onKeyDown()
Documentation onKeyUp().
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
/*
* without call to super onBackPress() will not be called when
* keyCode == KeyEvent.KEYCODE_BACK
*/
return super.onKeyUp(keyCode, event);
}
Also another reason that onBackPressed() may not be called is because you are using the soft back button on the actionbar, it that case the following is needed:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You are missing, super.onBackPressed();
#Override
public void onBackPressed() {
super.onBackPressed();
}
or you can use
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//replaces the default 'Back' button action
if(keyCode==KeyEvent.KEYCODE_BACK) {
// something here
finish();
}
return true;
}
thanks
make sure you are not calling onkeydown in your super view as it handles the back button clicking first.
working fine onKeyDown function return type false;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return false;
}
For whoever is wondering, as most functionality is deprected API 30>, the following will surely help you a lot.
public class MainActivity extends AppCompatActivity {
private OnBackPressedCallback onBackPressedCallback;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onBackPressedCallback = new OnBackPressedCallback(true) {
#Override
public void handleOnBackPressed() {
// Your business logic to handle the back pressed event
Log.d(TAG, "onBackPressedCallback: handleOnBackPressed");
}
};
getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback);
}
}
Just Remove super.onBackPressed() it will work

How to save only after characters are typed in a note app?

I'm new to android development. Literally a few days in and I built a simple note taking app. I would like for the notes to only save after I type something. So if my layout is empty I don't want it to save after I press the back button.
My current code is below. Any suggestions would be greatly appreciated.
#Override
public void onBackPressed() {
saveAndFinish();
}
#Override
public void onBackPressed() {
if (! layout.isEmpty() ) {
saveAndFinish();
}
}
I made up this method layout.isEmpty(). Change
layout.isEmpty() with the appropriate check.
Check if there is something typed in:
#Override
public void onBackPressed() {
EditText et = (EditText)findViewById(R.id.yourEditText);
if (et.length() > 0) {
saveAndFinish();
}
super.onBackPressed();
}
As a note, you might want to check if !et.getText().toString().trim().equals("") just to make sure that it will not save whitespace.
Just check if there is any value. Assuming you have one EditText that the user types in
#Override
public void onBackPressed()
{
if (!myEditText.getText.toString().equals(""))
{ saveAndFinish(); }
super.onBackPressed();
}

Categories

Resources