Convert Website Into An Android App | #5 Go back functionality and Animation
рдЕрдЧрд░ рдЖрдк рдЪрд╛рд╣рддреЗ рд╣реИрдВ рдХреА User Back рдХрд░реЗ рддреЛ рдкрд┐рдЫрд▓реЗ Page рдкрд░ рдЪрд▓рд╛ рдЬрд╛рдпреЗ рдФрд░ рдЕрдЧрд░ Last Page рдЖ рдЬрд╛рдпреЗ рддреЛ User рд╕реЗ рдкреВрдЫреЗ "Are you sure. You want to close this app" рдФрд░ Yes or No рдХрд╛ option рдЖ рдЬрд╛рдпреЗ! рддреЛ рдирд┐рдЪреЗ рдЙрд╕рдХрд╛ Process рджрд┐рдпрд╛ рдЧрдпрд╛ рд╣реИ!
1. рдирд┐рдЪреЗ рдореЗрдВ рджрд┐рдП рдЧрдП Code рдХреЛ MainActivity.java (app > java > domain > MainActivity.java)
рдореЗрдВ Image рдореЗ рджрд┐рдЦрд╛рдП рд╣реБрдП рд╕реНрдерд╛рди рдкрд░ Paste рдХреАрдЬрд┐рдпреЗ!
//set back button functionality
@Override
public void onBackPressed() { //if user presses the back button do this
if (webview.isFocused() && webview.canGoBack()) { //check if in webview and the user can go back
webview.goBack(); //go back in webview
} else { //do this if the webview cannot go back any further
new AlertDialog.Builder(this) //alert the person knowing they are about to close
.setTitle("EXIT")
.setMessage("Are you sure. You want to close this app?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
рдЕрдм рдореЗрд░рд╛ MainActivity.java File Change рдХреЗ рдмрд╛рдж Complete рдЗрд╕ рддрд░рд╣ рджрд┐рдЦрд╛рдИ рджреЗ рд░рд╣рд╛ рд╣реИ!
package com.example.technicalsangrah;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
String websiteURL = "https://youtube.com/technicalsangrah"; // sets web url
private WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if( ! CheckNetwork.isInternetAvailable(this)) //returns true if internet available
{
//if there is no internet do this
setContentView(R.layout.activity_main);
//Toast.makeText(this,"No Internet Connection, Chris",Toast.LENGTH_LONG).show();
new AlertDialog.Builder(this) //alert the person knowing they are about to close
.setTitle("No internet connection available")
.setMessage("Please Check you're Mobile data or Wifi network.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
//.setNegativeButton("No", null)
.show();
}
else
{
//Webview stuff
webview = findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl(websiteURL);
webview.setWebViewClient(new WebViewClientDemo());
}
}
private class WebViewClientDemo extends WebViewClient {
@Override
//Keep webview in app when clicking links
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
//set back button functionality
@Override
public void onBackPressed() { //if user presses the back button do this
if (webview.isFocused() && webview.canGoBack()) { //check if in webview and the user can go back
webview.goBack(); //go back in webview
} else { //do this if the webview cannot go back any further
new AlertDialog.Builder(this) //alert the person knowing they are about to close
.setTitle("EXIT")
.setMessage("Are you sure. You want to close this app?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
}
class CheckNetwork {
private static final String TAG = CheckNetwork.class.getSimpleName();
public static boolean isInternetAvailable(Context context)
{
NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null)
{
Log.d(TAG,"no internet connection");
return false;
}
else
{
if(info.isConnected())
{
Log.d(TAG," internet connection available...");
return true;
}
else
{
Log.d(TAG," internet connection");
return true;
}
}
}
}
.
bhai jab mai app mai 2-3 baar page kholta hu fir back button click krta hu to wo loop ho jata hai exit nhi hota. Agar mai homepage se back button use kru to exit hota but 2-3 page kholne k baad nhi hota.
ReplyDelete