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.
Related
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);
}
}
I have implemented press back twice to exit an activity. But the problem is i have to copy and paste the same code in every activity to make it work for every activity.
I can't make a common class and put my implementation because activities already extends AppCompatActivity, and as per i know; multiple inheritance is not supported.
So how do i do this
This is my implementation, suggestions are welcome.
boolean doubleBackToExitPressedOnce = false;
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
You can make your own custom Activity that extends AppCompatActivity and put your implementation in there and then let each of your other activities extend that custom Activity instead of AppCompatActivity. this is not multiple inheritence
Create your own custom (BaseActivity extends AppCompatActivity) and put your OnBackPressed() implementation in BaseActivity and then extend all activities from BaseActivity.
Don't listen to people telling you to create a static method. Instead, create a custom Activity class and make all of your classes extend that class. In that class put your custom onBackPressed functionality.
Another option is to go single activity with multiple fragments where you only need the back press implementation in one place.
First override back click listener:
#Override
public void onBackPressed() {
super.onBackPressed();
}
remove the super.onBackPressed();
add this code:
private static final int MIN_TIME_INTERVAL_BETWEEN_BACK_CLICKS = 2000; // # milliseconds, desired time passed between two back presses.
private long backPressedTime;
#Override
public void onBackPressed() {
if (backPressedTime + MIN_TIME_INTERVAL_BETWEEN_BACK_CLICKS > System.currentTimeMillis()) {
finishAffinity();
return;
}
else {
Toast.makeText(this, "Tap back button in order to exit", Toast.LENGTH_SHORT).show();
}
backPressedTime = System.currentTimeMillis();
}
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
}
}
I have main activity and 2 fragments on it, fragment1 call fragment2
When I am clicking the back button from the built in android buttons it call the onBackPressed function that I overrided in the main activity and exit me from the app.
I want that when I click back on the fragment 2 the onBackPressed of the main activity will not called and it will back me to fragment 1.
The back button of the action bar works well (I think it mean that the backstack is ok).
I tried to do:
getActivity().getSupportFragmentManager().beginTransaction().addToBackStack("Messages");
on the fragment 2
And tried to add
getSupportFragmentManager().popBackStack();
to the onBackPressed() of the main activity
It dosen't did nothing
Thank you
EDIT:
I am trying to do
public void onBackPressed() {
// getSupportFragmentManager().popBackStack();
android.support.v4.app.Fragment fragment2 = getSupportFragmentManager().findFragmentByTag(null);
if (fragment2 != null && fragment2.isVisible()) {
getSupportFragmentManager().popBackStack();
return;
}
and before any show
getSupportFragmentManager().beginTransaction().addToBackStack(null).commit();
tr.show(Fragment2);
but got null on the findFragment(null)
EDIT:
I checked getSupportFragmentManager().beginTransaction().mTail.fragment and saw that the value is the Fragment 1 that I want to be showed, but when I do getSupportFragmentManager().popBackStack(); it dosen't nothing.
Override onBackPressed() in Main activity.
Check the currently displayed fragment, if fragment2 is displayed, then pop back stack.
example:
#Override
public void onBackPressed() {
Fragment fragment2 = getFragmentManager().findFragmentByTag("tag");
if (fragment2 != null && fragment2.isVisible()) {
getFragmentManager().popBackStack();
} else {
finish();
}
}
just use below way; in your Activity check this
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Fragment fragment = getSupportFragmentManager().findFragmentByTag("SecondFragment");
if (fragment instanceof SecondFragment) {
// show first fragment
}else{
// finish();
}
}
i have used findFragmentByTag, you can also use findFragmentById
i agree with #Minhtdh comment but it only applicable if you replace one over other
Before calling fragment2.show() in the MainActivity, add the fragmnet transaction to backstack. There is no need to override onBackPressed().By default android implements the same functionality for you.
simple working code.
to get back to 1st fargment.
yourviewvariable.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
Sorry that I miss typed, I mean method onBackPressed.
But for your case, because you use FragmentTransition.show(), it will not add the fragment to backstack and popBackStack won't work. So I think you should try this:
1. don't called getSupportFragmentManager().beginTransaction().addToBackStack(null).commit(); because it add an unnecessary backstack.
2. don't store the "tr" variable as global variable, and you also need call commit() to end the transaction:
Fragment2.setTag("fragment2");
getSupportFragmentManager().beginTransaction().show(Fragment2).commit();
3. in onBackPressed():
android.support.v4.app.Fragment fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2");
if (fragment2 != null && fragment2.isVisible()) {
getSupportFragmentManager().beginTransaction().hide(fragment2).commit();
return;
} else {
super.onBackPressed();
}
Hope this help.
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();
}