how to achieve something like “finish” in a non-activity class in android?

▼魔方 西西 提交于 2019-12-10 14:52:45

问题


This dialog asks whether you want to install some other app...so when onclicked no button it must go back to the previous screen

    downloadDialog.setNegativeButton(stringButtonNo,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                         finish();
                }
            });

this gives the error:

The method finish() is undefined for the type new DialogInterface.OnClickListener(){}

how can i achieve what i wanted???

package com.Android.barcode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class BarcodeActivity extends Activity {
    public static String upc;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentIntegrator.initiateScan(this);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case IntentIntegrator.REQUEST_CODE: {
            if (resultCode != RESULT_CANCELED) {
                IntentResult scanResult = IntentIntegrator.parseActivityResult(
                        requestCode, resultCode, data);
                if (scanResult != null) {
                    upc = scanResult.getContents();

                    Intent intent = new Intent(BarcodeActivity.this, BarcodeResult.class);
                    startActivity(intent);
                    // put whatever you want to do with the code here
/*                  TextView tv = new TextView(this);
                    tv.setText(upc);
                    setContentView(tv);*/
                }
            }
            break;
        }
        }
    }
}

回答1:


Since you don't want to create that dialog from that activity : You have two options

1) Call an Intent back to the activity you want the user to go to.

Intent intent = new Intent(getBaseContext(), theActivity.class); 
getApplication().startActivity(intent) ;

or Else

2) Create a constructor for that class consisting of the dialog.

public class ABC {
    Context iContext=null;
   public ABC(Context con){
    iContext=con;
   }
 ....

}

Call the class with the Context of the activity. Like ABC(Cont) .And then use ((Activity)iContext).finish() within that class to finish that activity as you wish.




回答2:


if Your class having constructor which having Context assign in it than u can Use this way

   AlertDialog.Builder adb=new AlertDialog.Builder(context);
                adb.setTitle("Are You Sure Want To Delete?");
                adb.setPositiveButton("OK", new AlertDialog.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                    }});
                adb.setNegativeButton("CANCEL", new AlertDialog.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        ((Activity) context).finish();
                    }});
                adb.show();



回答3:


The method finish() is undefined for the type new DialogInterface.OnClickListener(){}

It is likely to give this error because DialogInterface.OnClickListener doesn't have any such method. If you want to finish your Activity you have to use

ActivityName.this.finish();



回答4:


Best solution is use dialog fragment for any type of dialog it will just open a dialog on your activity. And on listeners remove this dialog. Please have a look on the following link:

http://developer.android.com/reference/android/app/DialogFragment.html

It is recommended from Android guys as well.



来源:https://stackoverflow.com/questions/10662372/how-to-achieve-something-like-finish-in-a-non-activity-class-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!