How to Handle onback on fragment's webview in android - java

In my fragment activity am using the webview.when i am clicking on the hyperlink on the webview its redirecting to the specified url its fine.but the problem is when i am clicking on back it is redirected to the home fragment instead of the previos page.Can anyone help me how to redirect to the previous page on the webview.Below is my code in the mainactivity where i am handling the fragment onback press events.
private void selectItem(int position) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Locate Position
Fragment fragment = null;
switch (position) {
case 0:
//ft.replace(R.id.content_frame, frag,"home");
fragment=new Fagmentmain();
/*startActivity(new Intent(MainActivity.this,Category.class));*/
break;
case 1:
//ft.replace(R.id.content_frame, webfrag,"web");
fragment=new WebFragment();
/*Intent intent=new Intent(MainActivity.this,News.class);
intent.putExtra("pos",0);
startActivity(intent);*/
//startActivity(new Intent(MainActivity.this,News.class));
break;
case 2:
//ft.replace(R.id.content_frame, aboutfrag,"about");
Intent intent1=new Intent(MainActivity.this,News.class);
intent1.putExtra("pos",1);
startActivity(intent1);
break;
case 3:
Intent intent2=new Intent(MainActivity.this,News.class);
intent2.putExtra("pos",2);
startActivity(intent2);
break;
}
/*ft.commit();
mDrawerList.setItemChecked(position, true);
setTitle(title[position]);
mDrawerLayout.closeDrawer(mDrawerList);*/
if(fragment != null){
FragmentManager fragmanager=getSupportFragmentManager();
fragmanager.beginTransaction().replace(R.id.content_frame,fragment).addToBackStack("").commit();
mDrawerList.setItemChecked(position, true);
setTitle(title[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
FragmentManager fm = getSupportFragmentManager();
//FragmentTab1 frag=(FragmentTab1)getSupportFragmentManager().findFragmentById(arg0)
if (fm.getBackStackEntryCount() > 0) {
Log.e("back stack entry", fm.getBackStackEntryCount() + "");
fm.popBackStack();
}
}

solved the problem by adding following code in my Fragment
webview.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack() ){
webview.goBack();
return true;
}
return false;
}
});

Related

When I click the bottom navigation item more times the fragment loading again

This is my code I have created a bottom navigation view and I am connected the items to fragment separately. When I click the the navigation item more time the fragment load again again.
I don't know what is the problem here.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment SelectedFragment=null;
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.commit();
switch (item.getItemId()) {
case R.id.navigation_home:
setTitle("Playing11");
HomeFragment fragment = new HomeFragment();
ft.replace(R.id.fragment_container, fragment);
return true;
case R.id.navigation_dashboard:
HomeFragment1 fragment1 = new HomeFragment1();
ft.replace(R.id.fragment_container, fragment1);
return true;
case R.id.navigation_notifications:
setTitle("Safe11");
HomeFragment2 fragment2 = new HomeFragment2();
ft.replace(R.id.fragment_container, fragment2);
return true;
case R.id.navigation_notification:
setTitle("More");
HomeFragment3 fragment3 = new HomeFragment3();
ft.replace(R.id.fragment_container, fragment3);
return true;
}
return false;
}
};
}
Try to change your code like this:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.menu_home:
HomeFragment homeFragment = new HomeFragment();
fragmentTransaction.replace(R.id.frameContentPlace, homeFragment, "HomeFragment").addToBackStack("HomeFragment");
break;
case R.id.menu_profile:
HomeFragment1 fragment1 = new HomeFragment1();
fragmentTransaction.replace(R.id.frameContentPlace, fragment1).addToBackStack("HomeFragment1");
break;
case R.id.menu_bookings:
HomeFragment2 fragment2 = new HomeFragment2();
fragmentTransaction.replace(R.id.frameContentPlace, fragment2).addToBackStack("HomeFragment2");
break;
}
fragmentTransaction.commit();
return true;
}
When you adding fragment use tag. So write
ft.replace(R.id.fragment_container, fragment, "MyFragment);
instead of
ft.replace(R.id.fragment_container, fragment);
And check if this fragment is visible for not loading this fragment again
switch (item.getItemId())
{
case R.id.navigation_home:
HomeFragment fragment = (HomeFragment)getFragmentManager().findFragmentByTag("MyFragment");
if (fragment != null && !fragment.isVisible())
{
setTitle("Playing11");
HomeFragment fragment = new HomeFragment();
ft.replace(R.id.fragment_container, fragment);
return true;
}
}

How to use onBackPressed in BottonNavigationView?

How can I use onBackPressed in BottonNavigationView to select an item? For example, if I press item 2 I go to fragment number 2, but when I push back, I go back to fragment number 1 and item 2 is still selected, item 1 is not selected which is the one corresponding to fragment 1 ...
This is my code
private BottomNavigationViewEx.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.mapa:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.taxi:
transaction.replace(R.id.content, new TaxiFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.help:
transaction.replace(R.id.content, new HelpFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.go:
transaction.replace(R.id.content, new GoFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
}
return false;
}
};
}
UPDATE
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content, new MapFragment()).commit();
BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottomViewBar);
bottomNavigationViewEx.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
bottomNavigationViewEx.setSelectedItemId(R.id.mapa);
MenuItem item = bottomNavigationViewEx.getMenu().getItem(0);
if (!item.isChecked()) item.setChecked(true);
}
private BottomNavigationViewEx.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.mapa:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.taxi:
transaction.replace(R.id.content, new TaxiFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.help:
transaction.replace(R.id.content, new HelpFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.go:
transaction.replace(R.id.content, new GoFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
}
return false;
}
};
#Override
public void onBackPressed () {
if (mCurrentItem > 0) {
mCurrentItem--;
showFragment (mCurrentItem);
} else {
super.onBackPressed();
}
}
private void showFragment(int currentItem) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (currentItem) {
case 0:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 1:
transaction.replace(R.id.content, new TaxiFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 2:
transaction.replace(R.id.content, new HelpFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 3:
transaction.replace(R.id.content, new GoFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
}
}
}
I have this now, but I don't know if is correct
Keep a variable to denote which Fragment is your current. Inside onBackPressed, check what is the current Fragment, and decide where do you want to navigate to.
#Override
public void onBackPressed() {
if (mCurrentItem > 0) {
mCurrentItem--;
showFragment(mCurrentItem);
} else {
super.onBackPressed();
}
}
Your showFragment(mCurrentItem) can show different page based on the value, or however you'd like it to.
private void showFragment(int currentItem) {
switch (currentItem) {
case 0:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 1:
...
}
}
Then inside your showFragment(currentItem) method, add this code to select the item on BottomNavigationView:
MenuItem item = mBottomNavigationView.getMenu().getItem(mCurrentItem);
if (!item.isChecked()) item.setChecked(true);
Solve my problem by adding this line inside the class of my corresponding fragment in the onCreateView method, so when pressing back the item is selected.
BottomNavigationViewEx navigation =
getActivity().findViewById(R.id.YourNavigationId);
Menu drawer_menu = navigation.getMenu();
MenuItem menuItem;
menuItem = drawer_menu.findItem(R.id.YourItemId);
if(!menuItem.isChecked())
{
menuItem.setChecked(true);
}

Encountering Unreachable statement in switch case while implementing Firebase Auth

"Statement not reachable under the switch case for signing out using the button in menu item. "
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.sign_out_menu:
AuthUI.getInstance().signOut(this); //NOT REACHABLE
return true;
default:
return super.onOptionsItemSelected(item);
}
}
write this line at the end of method
"return super.onOptionsItemSelected(item);"
Remove the first line in the method onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item); // Remove this line
switch(item.getItemId()){
}
}
Because you have a return statement in your code, everything under the 1st return statement which is not part of the switch is not reachable
you can use either return super.onOptionsItemSelected(item); or just true; after ending switch case, don't forget break; statement too.
int id = item.getItemId();
switch (id) {
case R.id.id1:
Fragment1 fragment1 = new Fragment1();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(((ViewGroup)getView().getParent()).getId() , fragment1);
fragmentTransaction.commit();
break;
case R.id.id2:
Fragment2 fragment2 = new Fragment2();
FragmentManager fragmentManager1 = getFragmentManager();
FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
fragmentTransaction1.replace(((ViewGroup)getView().getParent()).getId() , fragment2);
fragmentTransaction1.commit();
break;
}
return true;

Undefined onKeyDown inside SherlockFragment

Im getting the following error when using the below code
The method onKeyDown(int, KeyEvent) is undefined for the type SherlockFragment
public boolean onKeyDown(int KeyCode, KeyEvent event) {
if ((KeyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(KeyCode, event);
}
I would keep this kind of thing out of your fragment. The following code worked for me in my MainActivity with 4 tabs that each have their own web view.
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
int currPage = mPager.getCurrentItem();
WebView wv = null;
switch (currPage) {
case 0:
wv = (WebView) mPager.getChildAt(currPage).findViewById(R.id.webView1);
break;
case 1:
wv = (WebView) mPager.getChildAt(currPage).findViewById(R.id.webView2);
break;
case 2:
wv = (WebView) mPager.getChildAt(currPage).findViewById(R.id.webView3);
break;
case 3:
wv = (WebView) mPager.getChildAt(currPage).findViewById(R.id.webView4);
break;
}
if (wv != null) {
if (wv.canGoBack()) {
wv.goBack();
} else {
super.onBackPressed();
}
} else {
super.onBackPressed();
}
}
edit: I ran into trouble on my 4th tab and the solution for me was to set the webView in my fragment(Tab0.java) static. Then directly referencing it in my main activity.
switch (currPage) {
case 0:
wv = Tab0.webView;
break;
case 1:
wv = Tab1.webView;
break;
case 2:
wv = Tab2.webView;
break;
case 3:
wv = Tab3.webView;
break;
}
The Activity should manage the back button not the fragment. If you want the fragment to manage the onKeyDown, you can attach the OnKeyListener, on the view returned by getView()
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left, container, false);
view.setOnKeyListener( new OnKeyListener() {
#Override
public boolean onKey( View v, int keyCode, KeyEvent event ) {
// here your code
}
});
return view;
}
After done few research. i found this !
Hopefully its help.
root =(ViewGroup) inflater.inflate(R.layout.setting_f_other, container, false);
root.setOnKeyListener( new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event ) {
if (keyCode == KeyEvent.KEYCODE_BACK){
//Do something
}
return false;
}
});

Get tab position outside its class

I have a calendar application. I am using ActionBarSherlock and Tabs. I have also implemented Options Menus in the Action Bar. I want one Menu Item in the Action Bar to behave differently when the tabs are selected.
How do I get the tab position in the onOptionsItemSelected(MenuItem item) method so based on that I can code the menu item accordingly?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab Tab1 = bar.newTab();
ActionBar.Tab Tab2 = bar.newTab();
Tab1.setText("Tab1");
Tab2.setText("Tab2");
Tab1.setTabListener(new MyTabListener());
Tab2.setTabListener(new MyTabListener());
bar.addTab(Tab1);
bar.addTab(Tab2);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent newActivity;
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
newActivity = new Intent(this, Main.class);
newActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(newActivity);
return true;
case R.id.menu_today_date:
//if condition here based on tabs selected
CalendarFragment frag = new CalendarFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(android.R.id.content, frag);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
return true;
case R.id.menu_prefs:
newActivity = new Intent(this, Settings.class);
startActivityForResult(newActivity, requestCode);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private class MyTabListener implements ActionBar.TabListener
{
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(tab.getPosition()==0)
{
CalendarFragment frag = new CalendarFragment();
ft.replace(android.R.id.content, frag);
}
else
{
ConverterFragment frag = new ConverterFragment();
ft.replace(android.R.id.content, frag);
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
I couldn't get the tab position, but this is how I ended up doing it:
private int tabSelected = 0;
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(tab.getPosition()==0)
{
tabSelected = 0;
CalendarFragment frag = new CalendarFragment();
ft.replace(android.R.id.content, frag);
}
else
{
tabSelected = 1;
ConverterFragment frag = new ConverterFragment();
ft.replace(android.R.id.content, frag);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent newActivity;
// Handle item selection
switch (item.getItemId())
case R.id.menu_today_date:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (tabSelected == 0){
CalendarFragment frag = new CalendarFragment();
ft.replace(android.R.id.content, frag);
}
else {
ConverterFragment frag = new ConverterFragment();
ft.replace(android.R.id.content, frag);
}
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
default:
return super.onOptionsItemSelected(item);
}
}

Categories