Reputation: 394
*Solved I just had to publicly declare my webview variable for my back button class to work correctly. Thanks for the help
I've got a unique setup with the webview class and everything I've googled I've gotten nowhere.
I'm using buttons to launch webview's and I'm getting the issue that when I press the back button to return to my main app screen it will just kill the app. I have added some code in at the bottom to try and fix my issue but it has not worked.
btw the below code has been updated to what is currently working for me.
package my.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;
public class MobileAppActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
WebView wb;
public void onMyButtonClick01(View view)
{
Toast.makeText(this, "Pay your dues here!", Toast.LENGTH_SHORT).show();
wb = new WebView(this);
wb.loadUrl("http://www.link1.html");
setContentView(wb);
}
public void onMyButtonClick02(View view)
{
Toast.makeText(this, "Re-Sign here!", Toast.LENGTH_SHORT).show();
wb = new WebView(this);
wb.loadUrl("http://www.link2.html");
setContentView(wb);
}
public void onBackPressed () {
if(wb != null) {
if(wb.canGoBack()) {
wb.goBack();
} else {
setContentView(R.layout.main);
wb = null;
}
} else {
super.onBackPressed();
}
}
}
Upvotes: 0
Views: 2182
Reputation: 8176
I assume you're saving off wb
elsewhere? Not sure how you're accessing that from onBackPressed()
. Let's say you do have a way to fetch it.
public void onBackPressed () {
if(wb != null) {
if(wb.canGoBack()) {
wb.goBack();
} else {
setContentView(R.layout.main);
wb = null;
}
} else {
super.onBackPressed();
}
}
Upvotes: 1