HamidTB HamidTB - 5 months ago 17
Android Question

Closing the application from a fragment causes returning to the parent activity

I tried several ways to end application from inside of a fragment, like:

system.exit(1)
android.os.Process.killProcess(android.os.Process.myPid());
getActivity().finish();


and it is all of fragment code;Exit code put on exitDialog.setPositiveButton oncliklistener that in my try cuse backing to parent activity instead exit

package com.TB.mylistprojct;

import android.os.Bundle;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class ActFooter extends Fragment
{
View EMyView =null;

Button BtnExit =null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater Inflater,ViewGroup Container,Bundle SavedInstanceState)
{
View MyView=Inflater.inflate(R.layout.actfooter, Container,false);
EMyView=MyView;
InitialUI();
return MyView;
}

public void InitialUI()
{
BtnExit=(Button)EMyView.findViewById(R.id.Btn_exit);
BtnExit.setOnClickListener(BtnExit_OnClick);
}

public OnClickListener BtnExit_OnClick=new OnClickListener()
{

@Override
public void onClick(View arg0)
{
AlertDialog.Builder exitDialog=new AlertDialog.Builder(getActivity());

exitDialog.setTitle("Warning");
exitDialog.setMessage("Exit Program");
exitDialog.setPositiveButton("YSE", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1)
{
// Exit Code PUT Here
}
});
exitDialog.setNegativeButton("NO", null);
exitDialog.show();
}
};
}


But the app returns to the parent activity

Answer

Finally i find the solution

first i add fllowing code in fragment

Intent intent = new Intent(getActivity(), ACTMain.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("LOGOUT", true);
                startActivity(intent);

                getActivity().finish();

and in main activity(in onCreate function) i check status with if statement and exit :)

        if (getIntent().getBooleanExtra("LOGOUT", false))
    {
        finish();
    }
Comments