Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call finish() from static method

I am using the Facebook Android SDK and want to close my Activity after a user logs in and gets the user object. In practice I am storing parts of it but I want to close the activity regardless.

      // make request to the /me API
      Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

        // callback after Graph API response with user object
        @Override
        public void onCompleted(GraphUser user, Response response) {
          if (user != null) {
           finish(); // causes errors
          }
        }
      });

The IDE error message on finish() is: "Cannot make a static reference to the non-static method finish() from the type Activity"

how to proceed?

like image 252
CQM Avatar asked May 06 '13 16:05

CQM


People also ask

What finish () function does?

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

What happens when you call finish () inside onCreate ()?

Within this onCreate() method if we call finish() method then it will execute the whole onCreate() method first and then it will execute the lifecycle method onDestroy() and the Activity gets destroyed.

Does finish call onPause?

Android will generally call onPause() if you call finish() at some point during your Activity's lifecycle unless you call finish() in your onCreate() .


1 Answers

Create a reference to your activity in onCreate with

//onCreate
final Activity activity = this;

Then you can use that in your onCompleted callback

activity.finish();

You might have to make Activity activity global.

EDIT 2/26/2014:

Note that calling finish() from a static method is probably bad practice. You are telling a specific instance of an Activity with it's own lifecycle that it should shut itself down from a static method, something without any lifecycle or state. Ideally you'd call finish() from something with a binding to the Activity.

like image 81
bclymer Avatar answered Oct 18 '22 20:10

bclymer