how to achieve something like "finish" in a non-activity class in android?
Asked Answered
P

4

5

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;
        }
        }
    }
}
Pinery answered 19/5, 2012 at 4:47 Comment(8)
do you want to close the dialog or activity?Whitewall
What are you trying to finish exactly? If it is an activity, then you need pass the activity's context to this non-activity class and call finish on it properly.Colossians
@ pretobomba that dint work either...similar errorPinery
@ mak right now when i click on no button it gives me a blank screen....when i click the back button on my keypad...it gives me the previous screen from where on clicking a button i got this dialog...what i want now is i dont what that blank screen when i click no,...it must go back directly to the previous screen 4m where i got the dialogPinery
Show your full alert dialogue codePatroon
Are you starting a new activity for presenting the dialog? it seems like this you starting new activity when you want to display the dialog. do you?Whitewall
You want to go to previous activity on close of this dialog right?? If you are opening the dialog only then there is no need to use finish because dialog is just added on your activity. It might be possible that you are opening this dialog by calling next activity.Scarce
@Archie.bpgc What is this!. :-\Doubleheader
D
9

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.

Doubleheader answered 19/5, 2012 at 5:6 Comment(0)
K
6

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();
Kidding answered 19/5, 2012 at 5:8 Comment(2)
@Archie.bpgc DUDE!, you need to have the context in the class. Which you can pass into the class through constructor ,like i have told in my answer.Doubleheader
ya as given in ngen answer context is define for class ABC as public ABC(Context con){ iContext=con; } if ur class contain this way constructor than u can go back to previous activity as shown in my [email protected]Kidding
M
2

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();
Mishandle answered 19/5, 2012 at 4:56 Comment(15)
but this dialog is not in an activity...it is just in a class ...this gives me : ActivityName cannot be resolved to a typePinery
@Archie.bpgc Why are you calling a dialog from a non activity . You can do the same within the activity by creating that 'class' within the activity.Doubleheader
If the dialog is not in an Activity, how can you create instance if Dialog using AlertDialog.Builder builder = new AlertDialog.Builder(this); here this has to be the context of Activity where you want to show the Dialog.Mishandle
Pass this activity in the constructor of this dialog class and do as @Me and We suggested.Whitewall
yeah fine....this didnt ggave me any syntax error but it gives the blank screen again, i am using this...Intent intent = new Intent(a, someActivity.class);...where a is the Activity gives as input to the dialogPinery
@Archie.bpgc Wait a sec, You had the activity passed all Along!!... Call the damn a.finish() then!!Doubleheader
Intent intent = new Intent(a, someActivity.class) here you have to pass context of current Activity so make it Intent intent = new Intent(CurrentActivity.this, someActivity.class)Mishandle
yeah thats where my problem is.....currentActivity....where my dialog present is not an Activity its just a classPinery
@MeandWe He already has the parent activity. He can just close it. ;-)Doubleheader
Call a.finish() What's making you not do it?Doubleheader
yeah ngen it worked.... thanks a lot...and sorry because i am new to android could not understand quickyPinery
actually ActivityName.this.finish(); this is what me n we said which is the actual answer....which i couldn't understand... thanks 4 ur explanationPinery
i didnt use any context which u said in ur answerPinery
@Archie.bpgc You didn't get what i told at all. Leave it. Glad that you solved it. Only activity can call finish(). In my answer , i had passed the context of the activity through constructor and then called the Finish() on the activity having that context . But you already had the activity as 'a'. so the same applies. Now you've called a.finish() i presume?.Doubleheader
yeah i understood your solution but just like you said...as i already have my parent Activity no need of context..hence i used a.finish()Pinery
S
1

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.

Scarce answered 19/5, 2012 at 5:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.