ایجاد شرط خروج
سلام.
من یک کد خروج دارم.اما متاسفانه با زدن دکمه برگشت در گوشی آلرت ظاهر میشه.
من نمیخوام اینطوری باشه.
میخوام هر صفحه ای که دکمه بازگشت رو میزنم ابتدا بررسی کنه ببینه اگه فرگمنت یا اکتیویتی ای برای برگشت پیدا نکرد و به اکتیویتی اصلی رسید،اونوقت این آلرت ظاهر بشه
public void onBackPressed() {
//Display alert message when back button has been pressed
backButtonHandler();
return;
}
public void backButtonHandler() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
Navigation_View1.this);
// Setting Dialog Title
alertDialog.setTitle("هشدار");
// Setting Dialog Message
alertDialog.setMessage("آیا میل به خروج دارید؟");
// Setting Icon to Dialog
alertDialog.setPositiveButton("بله",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("خیر",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
با سلام
در اکتیویتی اصلی از این کد استفاده کنید (MainActivity را با اسم اکتیویتی خود عوض کنید)
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("آیا میل به خروج دارید؟") .setCancelable(false) .setPositiveButton("بله", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { MainActivity.this.finish(); } }) .setNegativeButton("خیر", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show();
موفق باشید
ممنون دوست من ولی بازم درست نشد
ببین مثلا من با منو به یک فرگمنت میرم.
وقتی دکمه برگشت رو میزنم میخوام بره صفحه اصلی(منظورم نمایش تصویر صفحه ابتدای منوی)
اما این کد شما همون صفحه ای که هستم با زدن دکمه خروج برام آلرت نشون میده-میخوام اول خاصیت برگشت پذیری ایجاد کنه.وقتی دیگه چیزی واسه برگشت پیدا نکرد آلرت نمایش داده بشه
کد زیر رو امتحان کنید ، توی این کد اگه اولین فرگمنت بود دیالوگ خروج رو نمایش بده و اگه نبود یه فرگمنت بره به عقب
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
addBottomDots(position);
//layouts.length ==> تعداد فرگمنت ها
if (position == layouts.length - 4) {
// در اینجا در صورتی که به فرگمنت اول رسیدیم دیالوگ خروج رو نمایش بده
} else {
// کدهایی که مربوط به صفحه اول نیست و باید فرگمنت به عقب برگرده
int current = getItem(-1);
if (current > -1) {
viewPager.setCurrentItem(current);
} }
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) { }
@Override
public void onPageScrollStateChanged(int arg0) { }};
ممنون دوست من-فقط من این کد رو کجا بذارم؟؟؟؟
هرجا میذارم خطا میده!!!
بجای addBottomDots چی بنویسم؟؟
و layouts.length چی؟؟ این از کجا بدست میاد؟؟(من دکمه گرافیکی ندارم-از سخت افزار کمک میگیرم)
منظورتون از getItem دقیقا کدوم آیتمه؟اگه میشه یه خرده واضح تر بنویسید-من کارآموزم
ممون
راستش من بشدت از توضیحتون گیج شدم.
اگه میشه کد رو پایین میدم تغییرات رو اعمال کنین.ممنون
package soft.kaziwa.com.nab;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import sliding_tabhost.Tab1;
import sliding_tabhost.Tab10;
import sliding_tabhost.Tab12;
import sliding_tabhost.Tab13;
import sliding_tabhost.Tab14;
import sliding_tabhost.Tab3;
import sliding_tabhost.Tab4;
import sliding_tabhost.Tab5;
import sliding_tabhost.Tab6;
import sliding_tabhost.Tab7;
import sliding_tabhost.Tab8;
import sliding_tabhost.Tab9;
public class Navigation_View1 extends AppCompatActivity {
private Toolbar toolbar;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
ActionBarDrawerToggle mDrawerToggle;
//کلاس تشخیص اتصال
Boolean isInternetPresent = false;
//کلاس تشخیص اتصال
ConnectionDetector cd;
public void sendAppItself(Activity paramActivity, String paramStringToastMsg) throws IOException {
Toast.makeText(paramActivity, paramStringToastMsg, Toast.LENGTH_SHORT).show();
PackageManager pm = paramActivity.getPackageManager();
ApplicationInfo appInfo;
try {
appInfo = pm.getApplicationInfo(paramActivity.getPackageName(),
PackageManager.GET_META_DATA);
Intent sendBt = new Intent(Intent.ACTION_SEND);
sendBt.setType("*/*");
sendBt.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + appInfo.publicSourceDir));
Log.v("PACKAGEDIR: ", appInfo.publicSourceDir);
paramActivity.startActivity(Intent.createChooser(sendBt,
paramStringToastMsg));
} catch (PackageManager.NameNotFoundException e1) {
e1.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
final Button all = (Button) findViewById(R.id.btnAll);
Button ads22 = (Button) findViewById(R.id.btnInAds);
Button need22 = (Button) findViewById(R.id.btnInNeed);
cd = new ConnectionDetector(getApplicationContext());
assert all != null;
all.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
startActivityForResult(new Intent( Navigation_View1.this, Tab12.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), -1);
} else {
showAlertDialog(Navigation_View1.this, "No Internet Connection",
"ارتباط اینترنت برقرار نیست", false);
}
}
});
assert ads22 != null;
ads22.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
startActivityForResult(new Intent( Navigation_View1.this, Tab13.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), -1);
} else {
showAlertDialog(Navigation_View1.this, "No Internet Connection",
"ارتباط اینترنت برقرار نیست", false);
}
}
});
assert need22 != null;
need22.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
startActivityForResult(new Intent( Navigation_View1.this, Tab14.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), -1);
} else {
showAlertDialog(Navigation_View1.this, "No Internet Connection",
"ارتباط اینترنت برقرار نیست", false);
}
}
});
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
drawerLayout.closeDrawers();
switch (menuItem.getItemId()){
//Replacing the content with ContentFragment Which is our Inbox View;
case R.id.search:
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo == null) {
Toast.makeText(getApplicationContext(), "اوه!!اینترنت نداری!!!", Toast.LENGTH_LONG).show();
}
else {
getSupportFragmentManager().beginTransaction()
.add(R.id.frame,new Tab1(),"TAB1_FRAGMENT")
.addToBackStack("TAB1_FRAGMENT")
.commit();
}
return true;
case R.id.btn_Insert_ads:
ConnectivityManager connectivityManager3 = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo3 = connectivityManager3.getActiveNetworkInfo();
if (netInfo3 == null) {
Toast.makeText(getApplicationContext(), "اوه!!اینترنت نداری!!!", Toast.LENGTH_LONG).show();
}
else {
getSupportFragmentManager().beginTransaction()
.add(R.id.frame,new Tab3(),"TAB3_FRAGMENT")
.addToBackStack("TAB3_FRAGMENT")
.commit();
}
return true;
case R.id.btnInsertNeed:
ConnectivityManager connectivityManager4 = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo4 = connectivityManager4.getActiveNetworkInfo();
if (netInfo4 == null) {
Toast.makeText(getApplicationContext(), "اوه!!اینترنت نداری!!!", Toast.LENGTH_LONG).show();
}
else {
getSupportFragmentManager().beginTransaction()
.add(R.id.frame,new Tab4(),"TAB4_FRAGMENT")
.addToBackStack("TAB4_FRAGMENT")
.commit();
}
return true;
case R.id.btnVipAds:
ConnectivityManager connectivityManager5 = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo5 = connectivityManager5.getActiveNetworkInfo();
if (netInfo5 == null) {
Toast.makeText(getApplicationContext(), "اوه!!اینترنت نداری!!!", Toast.LENGTH_LONG).show();
}
else {
getSupportFragmentManager().beginTransaction()
.add(R.id.frame,new Tab5(),"TAB5_FRAGMENT")
.addToBackStack("TAB5_FRAGMENT")
.commit();
}
return true;
case R.id.btnSaleAds:
ConnectivityManager connectivityManager6 = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo6 = connectivityManager6.getActiveNetworkInfo();
if (netInfo6 == null) {
Toast.makeText(getApplicationContext(), "اوه!!اینترنت نداری!!!", Toast.LENGTH_LONG).show();
}
else {
getSupportFragmentManager().beginTransaction()
.add(R.id.frame,new Tab6(),"TAB6_FRAGMENT")
.addToBackStack("TAB6_FRAGMENT")
.commit();
}
return true;
case R.id.btnMortageAds:
ConnectivityManager connectivityManager7 = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo7 = connectivityManager7.getActiveNetworkInfo();
if (netInfo7 == null) {
Toast.makeText(getApplicationContext(), "اوه!!اینترنت نداری!!!", Toast.LENGTH_LONG).show();
}
else {
getSupportFragmentManager().beginTransaction()
.add(R.id.frame,new Tab7(),"TAB7_FRAGMENT")
.addToBackStack("TAB7_FRAGMENT")
.commit();
}
return true;
case R.id.btnFavicon:
ConnectivityManager connectivityManager8 = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo8 = connectivityManager8.getActiveNetworkInfo();
if (netInfo8 == null) {
Toast.makeText(getApplicationContext(), "اوه!!اینترنت نداری!!!", Toast.LENGTH_LONG).show();
}
else {
getSupportFragmentManager().beginTransaction()
.add(R.id.frame,new Tab8(),"TAB8_FRAGMENT")
.addToBackStack("TAB8_FRAGMENT")
.commit();
}
return true;
case R.id.btnSign:
ConnectivityManager connectivityManager9 = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo9 = connectivityManager9.getActiveNetworkInfo();
if (netInfo9 == null) {
Toast.makeText(getApplicationContext(), "اوه!!اینترنت نداری!!!", Toast.LENGTH_LONG).show();
}
else {
getSupportFragmentManager().beginTransaction()
.add(R.id.frame,new Tab9(),"TAB9_FRAGMENT")
.addToBackStack("TAB9_FRAGMENT")
.commit();
}
return true;
case R.id.btnCallMe:
ConnectivityManager connectivityManager10 = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo10 = connectivityManager10.getActiveNetworkInfo();
if (netInfo10 == null) {
Toast.makeText(getApplicationContext(), "اوه!!اینترنت نداری!!!", Toast.LENGTH_LONG).show();
}
else {
getSupportFragmentManager().beginTransaction()
.add(R.id.frame,new Tab10(),"TAB10_FRAGMENT")
.addToBackStack("TAB10_FRAGMENT")
.commit();
}
return true;
default:
Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show();
return true;
}
}
});
// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,null,R.string.openDrawer, R.string.closeDrawer){
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == R.id.drawer) {
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
} else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
}
return false;
}
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(mDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
mDrawerToggle.syncState();
}
@SuppressWarnings("deprecation")
public void showAlertDialog(Context context, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
//عنوان پنجره هشدار
alertDialog.setTitle(title);
//پیام پنجره هشدار
alertDialog.setMessage(message);
//آیکن پنجره هشدار
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
//دکمه OK
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// نمایش هشدار
alertDialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
//noinspection SimplifiableIfStatement
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.btnShare2) {
try {
sendAppItself(this, "شهروند رو به اشتراک بذار");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}else if (id == R.id.btnSign2) {
ConnectivityManager connectivityManager9 = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo9 = connectivityManager9.getActiveNetworkInfo();
if (netInfo9 == null) {
Toast.makeText(getApplicationContext(), "اوه!!اینترنت نداری!!!", Toast.LENGTH_LONG).show();
}
else {
getSupportFragmentManager().beginTransaction()
.add(R.id.frame,new Tab9(),"TAB9_FRAGMENT")
.addToBackStack("TAB9_FRAGMENT")
.commit();
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .