Error when finishing Activity through another class

Asked

Viewed 478 times

1

Can anyone explain why the error occurs LISTADO NO FINAL DA PERGUNTA, terminating the application, when I try to finalize the application through another class:

Classe1.class:

    ...
    ...
    AlertDialogManager alert = new AlertDialogManager();
    NetworkAvailable internet = new NetworkAvailable();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    if (!internet.isNetworkAvailable(this)){
    alert.showAlertDialog(MainActivity.this, "Sem internet!", false);

    if (alert.Saindo()){

       //O ERRO ACONTECE PELO FINISH(), EU QUERIA QUE ESSA FUNÇÃO EXECUTASSE
       //DEPOIS QUE EU CANCELASSE LÁ NA classe3.    
       //O app NEM INICIA, se eu deixar esse finish aqui, mas se eu o retirar,
       //O LOG executa:

       Log("Finalizado","Fechando a aplicação!")
            finish();

        }

    }

}




  }

classe2.class:

public class NetworkAvailable{

        public boolean isNetworkAvailable (final Context context) {
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = manager.getActiveNetworkInfo();


            boolean isAvailable = false;
            if(networkInfo != null && networkInfo.isConnected()){
                /* LARGURA E AUTURA GERALToast.makeText(this, "LARGURA > " + largura, Toast.LENGTH_LONG).show();*/
                Toast.makeText(context, "CONECTADO", Toast.LENGTH_LONG).show();
                isAvailable = true;
            } else {

                Toast.makeText(context, "NÃO CONECTADO", Toast.LENGTH_LONG).show();
                isAvailable = false;
            }
            return isAvailable;

        }
}

classe3.class:

public class AlertDialogManager {

    public boolean showAlertDialog(final Context context, final String title, final Boolean status) {


    if(status == false){
     AlertDialog.Builder builder = new AlertDialog.Builder(context);
     builder.setCancelable(false);
     builder.setTitle(title);

     builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {                       


             Log.v("CANCELANDO", "cancelado");
             showAlertDialog(context, title, true);
             Saindo();

         }
     });

     builder.setPositiveButton("Tentar novamente", new DialogInterface.OnClickListener(){
         @Override
         public void onClick(DialogInterface dialog, int which)
         {

            final ConnectivityManager conectivtyManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
            if (conectivtyManager.getActiveNetworkInfo() != null  
                    && conectivtyManager.getActiveNetworkInfo().isAvailable()  
                    && conectivtyManager.getActiveNetworkInfo().isConnected()) {  


            } 
             dialog.dismiss();
             showAlertDialog(context, title, false);
         }
     });
     AlertDialog dialog = builder.create(); // calling builder.create after adding buttons
     dialog.show();
    return status;
    }
    return status;  
    }

    public boolean Saindo(){

        Log.v("SAINDO", "saiu");        
        return true;

    }



}

01-26 22:17:05.721: E/WindowManager(25592): Activity ffitteste.favoritos.mapa.rafael.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@418654f0 that was originally added here
01-26 22:17:05.721: E/WindowManager(25592): android.view.WindowLeaked: Activity ffitteste.favoritos.mapa.rafael.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@418654f0 that was originally added here
01-26 22:17:05.721: E/WindowManager(25592):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:351)
01-26 22:17:05.721: E/WindowManager(25592):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:279)
01-26 22:17:05.721: E/WindowManager(25592):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
01-26 22:17:05.721: E/WindowManager(25592):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
01-26 22:17:05.721: E/WindowManager(25592):     at android.view.Window$LocalWindowManager.addView(Window.java:537)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.Dialog.show(Dialog.java:278)
01-26 22:17:05.721: E/WindowManager(25592):     at ffitteste.favoritos.mapa.rafael.AlertDialogManager.showAlertDialog(AlertDialogManager.java:48)
01-26 22:17:05.721: E/WindowManager(25592):     at ffitteste.favoritos.mapa.rafael.MainActivity.onCreate(MainActivity.java:103)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.Activity.performCreate(Activity.java:4465)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
01-26 22:17:05.721: E/WindowManager(25592):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 22:17:05.721: E/WindowManager(25592):     at android.os.Looper.loop(Looper.java:137)
01-26 22:17:05.721: E/WindowManager(25592):     at android.app.ActivityThread.main(ActivityThread.java:4514)
01-26 22:17:05.721: E/WindowManager(25592):     at java.lang.reflect.Method.invokeNative(Native Method)
01-26 22:17:05.721: E/WindowManager(25592):     at java.lang.reflect.Method.invoke(Method.java:511)
01-26 22:17:05.721: E/WindowManager(25592):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-26 22:17:05.721: E/WindowManager(25592):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-26 22:17:05.721: E/WindowManager(25592):     at dalvik.system.NativeStart.main(Native Method)
01-26 22:17:05.729: E/WindowManager(25592): Activity ffitteste.favoritos.mapa.rafael.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41776208 that was originally added here
01-26 22:17:05.729: E/WindowManager(25592): android.view.WindowLeaked: Activity ffitteste.favoritos.mapa.rafael.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41776208 that was originally added here
01-26 22:17:05.729: E/WindowManager(25592):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:351)
01-26 22:17:05.729: E/WindowManager(25592):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:279)
01-26 22:17:05.729: E/WindowManager(25592):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
01-26 22:17:05.729: E/WindowManager(25592):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
01-26 22:17:05.729: E/WindowManager(25592):     at android.view.Window$LocalWindowManager.addView(Window.java:537)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.Dialog.show(Dialog.java:278)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
01-26 22:17:05.729: E/WindowManager(25592):     at ffiteteste.favoritos.servicos.rafael.GPSTracker.showSettingsAlert(GPSTracker.java:194)
01-26 22:17:05.729: E/WindowManager(25592):     at ffitteste.favoritos.mapa.rafael.MainActivity.GPSzoom(MainActivity.java:819)
01-26 22:17:05.729: E/WindowManager(25592):     at ffitteste.favoritos.mapa.rafael.MainActivity.onCreate(MainActivity.java:164)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.Activity.performCreate(Activity.java:4465)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
01-26 22:17:05.729: E/WindowManager(25592):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 22:17:05.729: E/WindowManager(25592):     at android.os.Looper.loop(Looper.java:137)
01-26 22:17:05.729: E/WindowManager(25592):     at android.app.ActivityThread.main(ActivityThread.java:4514)
01-26 22:17:05.729: E/WindowManager(25592):     at java.lang.reflect.Method.invokeNative(Native Method)
01-26 22:17:05.729: E/WindowManager(25592):     at java.lang.reflect.Method.invoke(Method.java:511)
01-26 22:17:05.729: E/WindowManager(25592):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-26 22:17:05.729: E/WindowManager(25592):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-26 22:17:05.729: E/WindowManager(25592):     at dalvik.system.NativeStart.main(Native Method)
  • Someone manifests please kkkk

  • It could include the stacktrace that appears in the logcat?

  • They answered this: Make your function return a Boolean and Activity itself closes. In Android architecture vc should not keep instance of Activity in another class or Activity.

  • That’s right, @Wakim?

  • It doesn’t have that right or wrong, but it’s good practice not to expose your Activity for coupling purposes. Whenever you need to pass an instance of Activity to another class, think about Pattern Observer/Listener. But in practical terms, this code works*. It works if you instill the object NetworkAvailable before use (hehe).

  • How you’re spending Initiating the member saindo with the instance of the other Activity?

  • @Andréribeiro, all I have is this. It has how to make it work?

  • Without stacktrace and the rest of the code it is difficult to identify the problem.

  • I’ll edit the question.

Show 4 more comments

1 answer

3

Friend, the first problem here is: your method Saindo always returns true. Then Activity will call finish immediately, and will leave behind the Dialog (the stack trace is saying exactly that, that Activity went out and left a window).

I suggest you follow this example. It shows how to create a Dialog and an Interface, and how to make the Activity call the Dialog and implement this interface. Thus, a communication is established between your Activity and the Dialog.

If you use the Wizard creating new Andoid Studio Fragment (example here), it will create this scheme of Interface Callbacks. It is great to use for example (even because Dialogs Extend Fragments).

  • You’re right about everything you said, but when I return false, in the Saindo, this error also happens. How does it end BEFORE I EXECUTE the function? I want it to CANCEL ONLY when I click CANCEL FROM THE DIALOG.

  • To do what you want, you must establish a communication between Dialog and Activity. In my reply I cited examples of how to do this.

  • So the problem is only in communication? I can’t go out creating third class to manipulate activities with their functions?

  • Your class is not manipulating the Activity. Who is calling the finish is Activity itself. What happens is: if you want Activity to end when the user clicks the Cancel button, you must use a formal method of communication so that your code does not become a complete mess. Messy code makes it difficult for you to understand it yourself in the future, and can hide performance and security flaws.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.