Method of closing an Activity

Asked

Viewed 8,729 times

0

Good evening, in my application I made a Dialogfragment I put a function to close current Activity and go back to previous, the call put in the onBackPressed() and the code that makes this function is in the onclick of "OK"... only that it is presenting me an error in Logcat

Could someone help me?

Dialogfragment:

    package com.gif.popupsair;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;


public class MyDialog extends DialogFragment {

    private Activity getActivity;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builderr = new AlertDialog.Builder(getActivity());

        builderr.setMessage("Isso e um dialogFragment").setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                getActivity.finish();
            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builderr.setTitle("Hello Mundo");
        AlertDialog dialog = builderr.create();

        return dialog;
    }
}

Segunda Activity:

package com.gif.popupsair;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {

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

    public void AbrirDialog(View view){
       Toast.makeText(getApplicationContext(), "Aperte o botao para voltar", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBackPressed()
    {
        MyDialog di = new MyDialog();

        di.show(getSupportFragmentManager(), "my_dialog_tag");
    }
}

Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.gif.popupsair, PID: 1370
                  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.finish()' on a null object reference
                      at com.gif.popupsair.MyDialog$2.onClick(MyDialog.java:24)
                      at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Name of the first Activity: Mainactivity

Thank you...

1 answer

1


To close an Activity and "return" to the previous one, simply call the method Finish();

Done this on the second Activity, it will close the same and return to previous, in case your Activity "father" who called her.

Example Activity One

public class ActivityUm extends AppCompatActivity {

    @Override
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        setContentView(R.layout.activity_um);

        Intent intent = new Intent(ActivityUm.this, ActivityDois.class);
        startActivity(intent); //Abre a segunda activity
    }
}

Activity Two

public class ActivityDois extends AppCompatActivity {

    Button botao;

    @Override
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        setContentView(R.layout.activity_dois);

        botao = (Button)findViewById(R.id.botao);
        botao.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                finish(); // Finaliza essa activity e volta para anterior
            }
        });
    }
}

Remembering that when pressing the "back" button of the phone, is already called the method onDestroy() which has the function of "destroy" Activity, so when entering the second screen and pressing the back button of the cell phone, it will make the function of the method Finish() we associate with the click of a button on the screen.

So in this case, you can remove the method onBackPressed(), that it will perform the native function of returning and destroying this Activity.

  • So friend.... I created a popup that looks when you click the back button of the phone and that gives me the option to okay and Cancel... in the class at setPostive that would be the "OK" put the function onDestroy(); and it’s not going, why?

  • @Nathan, in class Mydialog and in the onClick() of the button OK call the method getActivity.finish();.

  • @Luídne mine worked that way - getActivity().finish();... Thanks for the help.

Browser other questions tagged

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