How to go to previous page in Webview present in a Fragment?












0















I have an actvity called SearchActivity. It has a searchbar and 4 tabs with Webviews. When I enter text and click on search, all the tabs are loading respective urls that I'm passing to them. But when I click on back button I want all the webviews to go back to the previous page, instead the activity closes. I've tried the below code, but it doesn't work. This the code of SearchActivity



public boolean onKeyDown(int keyCode, KeyEvent event) {

int count = getFragmentManager().getBackStackEntryCount();

if (count != 0 && (keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
getFragmentManager().popBackStack();
//additional code
} else {
super.onBackPressed();
}
return super.onKeyDown(keyCode, event);
}









share|improve this question























  • Check this stackoverflow.com/questions/10631425/…

    – Googler
    Nov 17 '18 at 12:57











  • I've used that one too. It doesn't work.

    – Dev Sharma
    Nov 17 '18 at 13:06











  • You have not added the callback of onBackPressed() in Fragment. Kindly follow this link and implement the onBackPressed into the fragment, stackoverflow.com/questions/5448653/…

    – Googler
    Nov 17 '18 at 13:21
















0















I have an actvity called SearchActivity. It has a searchbar and 4 tabs with Webviews. When I enter text and click on search, all the tabs are loading respective urls that I'm passing to them. But when I click on back button I want all the webviews to go back to the previous page, instead the activity closes. I've tried the below code, but it doesn't work. This the code of SearchActivity



public boolean onKeyDown(int keyCode, KeyEvent event) {

int count = getFragmentManager().getBackStackEntryCount();

if (count != 0 && (keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
getFragmentManager().popBackStack();
//additional code
} else {
super.onBackPressed();
}
return super.onKeyDown(keyCode, event);
}









share|improve this question























  • Check this stackoverflow.com/questions/10631425/…

    – Googler
    Nov 17 '18 at 12:57











  • I've used that one too. It doesn't work.

    – Dev Sharma
    Nov 17 '18 at 13:06











  • You have not added the callback of onBackPressed() in Fragment. Kindly follow this link and implement the onBackPressed into the fragment, stackoverflow.com/questions/5448653/…

    – Googler
    Nov 17 '18 at 13:21














0












0








0








I have an actvity called SearchActivity. It has a searchbar and 4 tabs with Webviews. When I enter text and click on search, all the tabs are loading respective urls that I'm passing to them. But when I click on back button I want all the webviews to go back to the previous page, instead the activity closes. I've tried the below code, but it doesn't work. This the code of SearchActivity



public boolean onKeyDown(int keyCode, KeyEvent event) {

int count = getFragmentManager().getBackStackEntryCount();

if (count != 0 && (keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
getFragmentManager().popBackStack();
//additional code
} else {
super.onBackPressed();
}
return super.onKeyDown(keyCode, event);
}









share|improve this question














I have an actvity called SearchActivity. It has a searchbar and 4 tabs with Webviews. When I enter text and click on search, all the tabs are loading respective urls that I'm passing to them. But when I click on back button I want all the webviews to go back to the previous page, instead the activity closes. I've tried the below code, but it doesn't work. This the code of SearchActivity



public boolean onKeyDown(int keyCode, KeyEvent event) {

int count = getFragmentManager().getBackStackEntryCount();

if (count != 0 && (keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
getFragmentManager().popBackStack();
//additional code
} else {
super.onBackPressed();
}
return super.onKeyDown(keyCode, event);
}






android android-fragments webview searchbar onbackpressed






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 '18 at 12:29









Dev SharmaDev Sharma

339




339













  • Check this stackoverflow.com/questions/10631425/…

    – Googler
    Nov 17 '18 at 12:57











  • I've used that one too. It doesn't work.

    – Dev Sharma
    Nov 17 '18 at 13:06











  • You have not added the callback of onBackPressed() in Fragment. Kindly follow this link and implement the onBackPressed into the fragment, stackoverflow.com/questions/5448653/…

    – Googler
    Nov 17 '18 at 13:21



















  • Check this stackoverflow.com/questions/10631425/…

    – Googler
    Nov 17 '18 at 12:57











  • I've used that one too. It doesn't work.

    – Dev Sharma
    Nov 17 '18 at 13:06











  • You have not added the callback of onBackPressed() in Fragment. Kindly follow this link and implement the onBackPressed into the fragment, stackoverflow.com/questions/5448653/…

    – Googler
    Nov 17 '18 at 13:21

















Check this stackoverflow.com/questions/10631425/…

– Googler
Nov 17 '18 at 12:57





Check this stackoverflow.com/questions/10631425/…

– Googler
Nov 17 '18 at 12:57













I've used that one too. It doesn't work.

– Dev Sharma
Nov 17 '18 at 13:06





I've used that one too. It doesn't work.

– Dev Sharma
Nov 17 '18 at 13:06













You have not added the callback of onBackPressed() in Fragment. Kindly follow this link and implement the onBackPressed into the fragment, stackoverflow.com/questions/5448653/…

– Googler
Nov 17 '18 at 13:21





You have not added the callback of onBackPressed() in Fragment. Kindly follow this link and implement the onBackPressed into the fragment, stackoverflow.com/questions/5448653/…

– Googler
Nov 17 '18 at 13:21












2 Answers
2






active

oldest

votes


















1














I would use an interface:



Public Interface InterfaceBackPressed {
onInterfaceBackPressed();
}


Then in your Activity:



public class SearchActivity extends Activity {
...
@Override public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
if (!(fragment instanceof InterfaceBackPressed) || !((InterfaceBackPressed) fragment).onBackPressed()) {
super.onInterfaceBackPressed();
}
}
}


and have every fragment implement your interface:



public class MyFragment extends Fragment implements IOnBackPressed{
@Override
public void onInterfaceBackPressed() {
if (webView.canGoBack()) {

webView.goBack();
} else {
super.onInterfaceBackPressed();
}
}
}





share|improve this answer
























  • I'm getting multiple errors

    – Dev Sharma
    Nov 18 '18 at 7:41



















1














1.Add an interface on SearchActivity and implement that interface on MyFragment to communicate between SearchActivity and MyFragment.



2.Override onBackPressed() method of SearchActivity to add custom functions when the back button is pressed and check if fragment's WebView can go back via goBack listener. If can go back, then do so. If can not, call super.onBackPressed().



SearchActivity.java



public class SearchActivity extends AppCompatActivity{

public interface goBack{
boolean canGoBack();
}

@Override
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
//fragment_container is a layout where fragment is populated

if (fragment instanceof goBack && fragment.canGoBack()) {
return;
}
super.onBackPressed();

}
}


MyFragment.java



public class MyFragment extends Fragment implements SearchActivity.goBack{
@Override
public boolean canGoBack() {
if (webView.canGoBack()) {

webView.goBack();
return true;
}
return false;
}





share|improve this answer


























  • The Webview is present in Fragment. How can I access it from the activity?

    – Dev Sharma
    Nov 17 '18 at 13:04











  • i edited my post, check again

    – Touhidul Islam
    Nov 17 '18 at 14:35











  • What is fragment_container?

    – Dev Sharma
    Nov 18 '18 at 7:38











  • see the comment where fragment_container is mentioned. Its a layout (like FrameLayout) id where fragment is populated

    – Touhidul Islam
    Nov 18 '18 at 10:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351280%2fhow-to-go-to-previous-page-in-webview-present-in-a-fragment%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I would use an interface:



Public Interface InterfaceBackPressed {
onInterfaceBackPressed();
}


Then in your Activity:



public class SearchActivity extends Activity {
...
@Override public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
if (!(fragment instanceof InterfaceBackPressed) || !((InterfaceBackPressed) fragment).onBackPressed()) {
super.onInterfaceBackPressed();
}
}
}


and have every fragment implement your interface:



public class MyFragment extends Fragment implements IOnBackPressed{
@Override
public void onInterfaceBackPressed() {
if (webView.canGoBack()) {

webView.goBack();
} else {
super.onInterfaceBackPressed();
}
}
}





share|improve this answer
























  • I'm getting multiple errors

    – Dev Sharma
    Nov 18 '18 at 7:41
















1














I would use an interface:



Public Interface InterfaceBackPressed {
onInterfaceBackPressed();
}


Then in your Activity:



public class SearchActivity extends Activity {
...
@Override public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
if (!(fragment instanceof InterfaceBackPressed) || !((InterfaceBackPressed) fragment).onBackPressed()) {
super.onInterfaceBackPressed();
}
}
}


and have every fragment implement your interface:



public class MyFragment extends Fragment implements IOnBackPressed{
@Override
public void onInterfaceBackPressed() {
if (webView.canGoBack()) {

webView.goBack();
} else {
super.onInterfaceBackPressed();
}
}
}





share|improve this answer
























  • I'm getting multiple errors

    – Dev Sharma
    Nov 18 '18 at 7:41














1












1








1







I would use an interface:



Public Interface InterfaceBackPressed {
onInterfaceBackPressed();
}


Then in your Activity:



public class SearchActivity extends Activity {
...
@Override public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
if (!(fragment instanceof InterfaceBackPressed) || !((InterfaceBackPressed) fragment).onBackPressed()) {
super.onInterfaceBackPressed();
}
}
}


and have every fragment implement your interface:



public class MyFragment extends Fragment implements IOnBackPressed{
@Override
public void onInterfaceBackPressed() {
if (webView.canGoBack()) {

webView.goBack();
} else {
super.onInterfaceBackPressed();
}
}
}





share|improve this answer













I would use an interface:



Public Interface InterfaceBackPressed {
onInterfaceBackPressed();
}


Then in your Activity:



public class SearchActivity extends Activity {
...
@Override public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
if (!(fragment instanceof InterfaceBackPressed) || !((InterfaceBackPressed) fragment).onBackPressed()) {
super.onInterfaceBackPressed();
}
}
}


and have every fragment implement your interface:



public class MyFragment extends Fragment implements IOnBackPressed{
@Override
public void onInterfaceBackPressed() {
if (webView.canGoBack()) {

webView.goBack();
} else {
super.onInterfaceBackPressed();
}
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 17 '18 at 12:59









Nikos HidalgoNikos Hidalgo

918215




918215













  • I'm getting multiple errors

    – Dev Sharma
    Nov 18 '18 at 7:41



















  • I'm getting multiple errors

    – Dev Sharma
    Nov 18 '18 at 7:41

















I'm getting multiple errors

– Dev Sharma
Nov 18 '18 at 7:41





I'm getting multiple errors

– Dev Sharma
Nov 18 '18 at 7:41













1














1.Add an interface on SearchActivity and implement that interface on MyFragment to communicate between SearchActivity and MyFragment.



2.Override onBackPressed() method of SearchActivity to add custom functions when the back button is pressed and check if fragment's WebView can go back via goBack listener. If can go back, then do so. If can not, call super.onBackPressed().



SearchActivity.java



public class SearchActivity extends AppCompatActivity{

public interface goBack{
boolean canGoBack();
}

@Override
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
//fragment_container is a layout where fragment is populated

if (fragment instanceof goBack && fragment.canGoBack()) {
return;
}
super.onBackPressed();

}
}


MyFragment.java



public class MyFragment extends Fragment implements SearchActivity.goBack{
@Override
public boolean canGoBack() {
if (webView.canGoBack()) {

webView.goBack();
return true;
}
return false;
}





share|improve this answer


























  • The Webview is present in Fragment. How can I access it from the activity?

    – Dev Sharma
    Nov 17 '18 at 13:04











  • i edited my post, check again

    – Touhidul Islam
    Nov 17 '18 at 14:35











  • What is fragment_container?

    – Dev Sharma
    Nov 18 '18 at 7:38











  • see the comment where fragment_container is mentioned. Its a layout (like FrameLayout) id where fragment is populated

    – Touhidul Islam
    Nov 18 '18 at 10:44
















1














1.Add an interface on SearchActivity and implement that interface on MyFragment to communicate between SearchActivity and MyFragment.



2.Override onBackPressed() method of SearchActivity to add custom functions when the back button is pressed and check if fragment's WebView can go back via goBack listener. If can go back, then do so. If can not, call super.onBackPressed().



SearchActivity.java



public class SearchActivity extends AppCompatActivity{

public interface goBack{
boolean canGoBack();
}

@Override
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
//fragment_container is a layout where fragment is populated

if (fragment instanceof goBack && fragment.canGoBack()) {
return;
}
super.onBackPressed();

}
}


MyFragment.java



public class MyFragment extends Fragment implements SearchActivity.goBack{
@Override
public boolean canGoBack() {
if (webView.canGoBack()) {

webView.goBack();
return true;
}
return false;
}





share|improve this answer


























  • The Webview is present in Fragment. How can I access it from the activity?

    – Dev Sharma
    Nov 17 '18 at 13:04











  • i edited my post, check again

    – Touhidul Islam
    Nov 17 '18 at 14:35











  • What is fragment_container?

    – Dev Sharma
    Nov 18 '18 at 7:38











  • see the comment where fragment_container is mentioned. Its a layout (like FrameLayout) id where fragment is populated

    – Touhidul Islam
    Nov 18 '18 at 10:44














1












1








1







1.Add an interface on SearchActivity and implement that interface on MyFragment to communicate between SearchActivity and MyFragment.



2.Override onBackPressed() method of SearchActivity to add custom functions when the back button is pressed and check if fragment's WebView can go back via goBack listener. If can go back, then do so. If can not, call super.onBackPressed().



SearchActivity.java



public class SearchActivity extends AppCompatActivity{

public interface goBack{
boolean canGoBack();
}

@Override
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
//fragment_container is a layout where fragment is populated

if (fragment instanceof goBack && fragment.canGoBack()) {
return;
}
super.onBackPressed();

}
}


MyFragment.java



public class MyFragment extends Fragment implements SearchActivity.goBack{
@Override
public boolean canGoBack() {
if (webView.canGoBack()) {

webView.goBack();
return true;
}
return false;
}





share|improve this answer















1.Add an interface on SearchActivity and implement that interface on MyFragment to communicate between SearchActivity and MyFragment.



2.Override onBackPressed() method of SearchActivity to add custom functions when the back button is pressed and check if fragment's WebView can go back via goBack listener. If can go back, then do so. If can not, call super.onBackPressed().



SearchActivity.java



public class SearchActivity extends AppCompatActivity{

public interface goBack{
boolean canGoBack();
}

@Override
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
//fragment_container is a layout where fragment is populated

if (fragment instanceof goBack && fragment.canGoBack()) {
return;
}
super.onBackPressed();

}
}


MyFragment.java



public class MyFragment extends Fragment implements SearchActivity.goBack{
@Override
public boolean canGoBack() {
if (webView.canGoBack()) {

webView.goBack();
return true;
}
return false;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 14:35

























answered Nov 17 '18 at 12:57









Touhidul IslamTouhidul Islam

1,2891415




1,2891415













  • The Webview is present in Fragment. How can I access it from the activity?

    – Dev Sharma
    Nov 17 '18 at 13:04











  • i edited my post, check again

    – Touhidul Islam
    Nov 17 '18 at 14:35











  • What is fragment_container?

    – Dev Sharma
    Nov 18 '18 at 7:38











  • see the comment where fragment_container is mentioned. Its a layout (like FrameLayout) id where fragment is populated

    – Touhidul Islam
    Nov 18 '18 at 10:44



















  • The Webview is present in Fragment. How can I access it from the activity?

    – Dev Sharma
    Nov 17 '18 at 13:04











  • i edited my post, check again

    – Touhidul Islam
    Nov 17 '18 at 14:35











  • What is fragment_container?

    – Dev Sharma
    Nov 18 '18 at 7:38











  • see the comment where fragment_container is mentioned. Its a layout (like FrameLayout) id where fragment is populated

    – Touhidul Islam
    Nov 18 '18 at 10:44

















The Webview is present in Fragment. How can I access it from the activity?

– Dev Sharma
Nov 17 '18 at 13:04





The Webview is present in Fragment. How can I access it from the activity?

– Dev Sharma
Nov 17 '18 at 13:04













i edited my post, check again

– Touhidul Islam
Nov 17 '18 at 14:35





i edited my post, check again

– Touhidul Islam
Nov 17 '18 at 14:35













What is fragment_container?

– Dev Sharma
Nov 18 '18 at 7:38





What is fragment_container?

– Dev Sharma
Nov 18 '18 at 7:38













see the comment where fragment_container is mentioned. Its a layout (like FrameLayout) id where fragment is populated

– Touhidul Islam
Nov 18 '18 at 10:44





see the comment where fragment_container is mentioned. Its a layout (like FrameLayout) id where fragment is populated

– Touhidul Islam
Nov 18 '18 at 10:44


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351280%2fhow-to-go-to-previous-page-in-webview-present-in-a-fragment%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings