How to achieve something like β€œfinish” in a class without activity in android?

This dialog box asks if you want to install any other application ... therefore, when the onclicked no button does not return, it should return to the previous screen

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

this gives an error:

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

How can I achieve what I need?

 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; } } } } 
+4
source share
4 answers

Since you do not want to create this dialog from this action: you have two options

1) Call Intent back to the action you want to go to.

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

or Else

2) Create a constructor for this class consisting of a dialog.

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

Call the class using an activity context. Like ABC(Cont) . And then use ((Activity)iContext).finish() inside this class to end this activity as you wish.

+8
source

if your class has a constructor that has a Context in it than u can use this method

  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(); 
+5
source

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

This error probably occurs because DialogInterface.OnClickListener does not have such a method. If you want to complete your activity, you must use

 ActivityName.this.finish(); 
+2
source

The best solution is to use a dialog fragment for any type of dialogue that simply opens the dialogue of your activity. And on listeners delete this dialogue. Please view the following link:

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

Also recommended for Android guys.

+1
source

Source: https://habr.com/ru/post/1413271/


All Articles