androidandroid-activityandroid-alertdialogrelaunch

relaunch application without changing


I found some code to press home button from code

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);

and This one :

  moveTaskToBack(true);

I have a activity that is in single task
And a alertDialog with a button then you click on the button one of those code run and go to home
so when you go to app again the alert dismissed before
I want to relaunch the app without dismissing alertDialog What can i do?
thanks


Solution

  • AlertDialog default button will dissmis dialog . you should create custom dialog with custom view

    public class CustomDialog extends AlertDialog(){
        TextView mTitleView,mDescriptionView;
        Button mYesButton,mNoButton,mCancelButton;
        boolean isYesButtonVisible = true;
        boolean isNoButtonVisible = true;
        boolean isCancelButtonVisible = true;
    
        public CustomDialog(Context context){
            super(context);
        }
    
        public void setYesButtonVisible(boolean isVisible){
            isYesButtonVisible = isVisible
        }
    
        public void setNoButtonVisible(boolean isVisible){
            isNoButtonVisible = isVisible
        }
    
        public void setCancelButtonVisible(boolean isVisible){
            isCancelButtonVisible = isVisible
        }
    
        @override
        public void show(){
            super.show();
            setContentView(R.layout.dialog_custom);
            mTitleView = findViewById(R.id.text_title);
            mDescriptionView = findViewById(R.id.text_description);
            mYesButton = findViewById(R.id.btn_yes);
            mNoButton = findViewById(R.id.btn_no);
            mCancelButton = findViewById(R.id.btn_cancel);
            isYesButtonVisible ? mYesButton.setVisibility(View.VISIBLE) : mYesButton.setVisibility(View.GONE);
            isNoButtonVisible ? mNoButton.setVisibility(View.VISIBLE) : mNoButton.setVisibility(View.GONE);
            isCancelButtonVisible ? mCancelButton.setVisibility(View.VISIBLE) : mCancleButton.setVisibility(View.GONE);
         }
        //must be called after dialog is showing else you will get NullPointerException
        public void setOnYesClickListener(View.OnClickListener listener){
            mYesButton.setonClickListener(listener);
        }
        public void setOnNoClickListener(View.OnClickListener listener){
            mNoButton.setonClickListener(listener);
        }
        public void setOnCancelClickListener(View.OnClickListener listener){
            mCancelButton.setonClickListener(listener);
        }
    }
    

    and layout dialog_custom can be designed by yourself too but this is my layout file for custom dialog

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/text_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:textColor="@color/black"
            android:textSize="18sp"
            android:textStyle="bold" />
    
        <TextView
            android:id="@+id/text_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:textSize="16sp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <TextView
                android:id="@+id/btn_yes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="8dp"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:paddingTop="8dp"
                android:text="@string/yes"
                android:textColor="@color/green_click"
                android:textSize="18sp"
                android:textStyle="bold" />
    
            <TextView
                android:id="@+id/btn_no"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:paddingBottom="8dp"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:paddingTop="8dp"
                android:text="@string/no"
                android:textColor="@color/green_click"
                android:textSize="18sp"
                android:textStyle="bold" />
    
            <TextView
                android:id="@id/btn_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="24dp"
                android:paddingBottom="8dp"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:paddingTop="8dp"
                android:text="@string/cancel"
                android:textColor="@color/green_click"
                android:textSize="18sp"
                android:textStyle="bold" />
        </LinearLayout>