Back when back button is pressed in webview - java

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 {
......
}
}

Related

BottomSheetBehavior is collsaping but backpress is not working why?

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);
}
}

the interstitial admob ad not showing on button click until i click back button

Hello there i have a problem i've added admob interstitial to my application when i click a button but the problem is when i click the button the ad doesn't shows until i clicked back button on the phone then the ad shows
the app has 2 activities and the button take u to another activity and i want the ad to show-up before the second activity appears
this is the code
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
final Intent intent = new Intent(MainActivity.this, Main2Activity.class);
tip1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Tips tip = (Tips) DAOTips.lt.get(1);
intent.putExtra("tip",tip.getTip());
intent.putExtra("title",tip.getTitle());
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
startActivity(intent);
}
});
The problem is that you are starting the ad but then immediately opening a new screen.
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
startActivity(intent);
after show is called startActivity is run. You need to remove startActivity otherwise it will be the top activity on the stack and block the ad!
There are two ways,
1) show the ad on oncreate of activity2
2) use admobs listeners. admob has a listener that lets you know when the ad has closed.
you can open the new activity once the ad has closed
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
}
#Override
public void onAdLoaded() {
}
#Override
public void onAdOpened() {
}
#Override
public void onAdClosed() {
//use this
}
});

Why is my overlay not shown (Android)?

I am currently developing an android app. I want to show a map (based on Google Maps) and with pressing a button an overlay should be shown.
So far the map is shown and the overlay is shown, too. BUT: The problem is that the overlay is shown if I press the button twice although the overlay should be invisible after pressing the button a second time. I tried to swap the view.setVisisiblity(View.VISISBLE) and the view.setVisisiblity(View.INVISIBLE) call but the result was almost the same...I used a TextureView to solve this problem.
public void openWindowOnButtonClick() {
view = (TextureView) findViewById(R.id.textureView);
FloatingActionButton fb = (FloatingActionButton) findViewById(R.id.floatingActionButton);
final InputMethodManager keyboard = (InputMethodManager) getSystemService(getBaseContext().INPUT_METHOD_SERVICE);
fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// check if the Overlay should be visible. If this value is true, it is not shown -> show it.
if (view.getVisibility() == View.INVISIBLE) {
view.setVisibility(View.VISIBLE);
view.bringToFront();
keyboard.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
} else {
view.setVisibility(View.INVISIBLE);
keyboard.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
});
}
This is my method to swap the overlay's status from visible to invisible and the other way around.
Hope anyone can help.
Thanks!
TextureView will not be available unless initializing SurfaceTexture , i don't know why textureview is available after the first click ,but you can use SurfaceView, or you can use TextureView With setting SurfaceTexture
public void openWindowOnButtonClick() {
view = (TextureView) findViewById(R.id.textureView);
view.setSurfaceTexture(new SurfaceTexture(GLES20.GL_ACTIVE_TEXTURE));
view.getSurfaceTexture().detachFromGLContext();
FloatingActionButton fb = (FloatingActionButton) findViewById(R.id.floatingActionButton);
final InputMethodManager keyboard = (InputMethodManager) getSystemService(getBaseContext().INPUT_METHOD_SERVICE);
fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// check if the Overlay should be visible. If this value is true, it is not shown -> show it.
if (view.getVisibility() == View.INVISIBLE) {
view.setVisibility(View.VISIBLE);
view.bringToFront();
view.getParent().requestLayout();
((View)view.getParent()).invalidate();
keyboard.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
} else {
view.setVisibility(View.INVISIBLE);
keyboard.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
});
}
but it will log error in monitor but will work with you

redirect to main window/screen in android device on clicking the back button

I am opening a web url within my android app using this code below. My challenge is that on opening a url, it loads the page but on after navigating the url then clicking the back button of the android device instead of the app going back to the previous screen, it exits the application. This is the code
mWebView = (WebView) findViewById(R.id.webview);
mWebView.loadUrl("http://weburl.com");
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
Please how can I make it in such a way that clicking the back button let it navigate to the previous screen of the web url.
public void onBackPressed(){
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
finish();
}
}
I suppose this is the solution you are looking for. This would move webview to previous webpage if it has a previous page, or else it would finish the activity
You should call onBackPressed() in the following manner in order to exit from the app:
public void onBackPressed(){
system.exit(0);
super.onBackPressed();
}

Android exit app on close

Hi in my android app I included a splash screen after this activity whenever I pressed the back button it is going to the previous pages of MainActivity. But I needed to exit from application if user press back button from MainActivity. But now currently mainactivity is not the start activity and splash screen is the activity. So i checked some methods and I saw
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this will clear the previous activities.. But in MainActivty why I need an intent.. I gave
finish();
in every intent of each activity so the activity will be cleared. But that is affecting my app's entire structure .. whenever user press back button app is going to home page. So any one suggest a solution.. so that I can exit directly from MainActivity if user presses a back button.
this is my backpress code for mainActivity
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
and my splashscreen
WebView wv;
// Splash screen timer
private static int SPLASH_TIME_OUT = 4000;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_flash);
wv = (WebView) findViewById (R.id.webView1);
wv.setHorizontalScrollBarEnabled(false);
wv.loadUrl("file:///android_asset/Finally-320.gif");
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
}
for your query:
But I needed to exit from application if user press back button from MainActivity.
For that you need to write this in MainActivity
#Override
public void onBackPressed() {
finish();
}
Update:
put this line to exit from app:
moveTaskBack(true);
use System.exit(0) in onBackPressed() like this:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
System.exit(0);
}

Categories

Resources