Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
        }
        }
    }
}
like image 365
Housefly Avatar asked May 19 '12 04:05

Housefly


People also ask

What does finish () do in Android?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do you call a startActivity from a non activity class?

public Context call mcontext;<br> // ..... <br> Intent intent = new Intent(call mcontext, AboutActivity. class);<br> call mcontext. startActivity(intent);

Which method is used to destroy an activity in Android?

The onStop() and onDestroy() methods get called, and Android destroys the activity.

Can we create instance of activity android?

Activity instances are always created by the Android system. This is because a lot of initializations have to be done for the activity to work. To create a new activity you call startActivity with an Intent describing the activity to start.


2 Answers

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.

like image 75
NT_ Avatar answered Nov 04 '22 16:11

NT_


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();
like image 25
Khan Avatar answered Nov 04 '22 15:11

Khan