Method of closing an Activity

0

Good night, in my application I made a DialogFragment put a function of closing current activity and return to previous, the call put in onBackPressed() and the code that does that function is in the onclick of "OK" ... only that You are introducing an error in LogCat

Could anyone 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;
    }
}

Second 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 ...

    
asked by anonymous 31.10.2016 / 00:37

1 answer

1

To close an activity and back to the previous activity, just call the finish ();

This is done in the second activity, it will close the same and will return to previous, in the case of its "parent" activity that called it.

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 by pressing the "back" button on the cell phone, the onDestroy () method is called that has the function to "destroy" the activity, therefore when entering the second screen and pressing the button finish () method that we associate with the click of a button on the screen.

So in this case you can remove the onBackPressed () method, which will perform the native function of going back and destroying this activity.

    
31.10.2016 / 19:59