0

Just to make it clear, this is not I want. I want to access another Activity's context.

Suppose I've two activities, MainActivity and WebActivity. In MainActivity I used oAuth2 login, and after login I start the WebActivity. In WebActivity I need to logout with the function mTencent.logout(MainActivity.this);, the question is how can I access MainActivity from WebActivity?

If I do this directly, I get the error,

MainActivity is not an enclosing class?

Considering I'm a starter of android, here may be not the exact way to implement it.

Will someone help? Thank you!

The API : void com.tencent.tauth.Tencent.logout(Context context)

Community
  • 1
  • 1
LF00
  • 27,015
  • 29
  • 156
  • 295

4 Answers4

1

Instead of using context of one activity in other which may result in crashes sometimes.
u can use libraries like EventBus to link the code.

Define a class which implements event u want to perform eg:LogOutEvent.java

public static class LogOutEvent { /* Additional fields if needed */ }

U can post events like logout from WebViewActivity.java using following command

EventBus.getDefault().post(new LogOutEvent());

and in MainActivity you first need to register event bus

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
}

and then in MainActivity you can subscribe for events like this

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(LogOutEvent event) {/* Do log out here */};
Victor
  • 4,171
  • 1
  • 29
  • 37
1

There is a good practice solution to your problem which involves certain steps to be performed:

1- Define an interface:

public interface LogOutInterface {
  public void logout();
}

2- Have your MainActivity implement this interface:

public class MainActivity extends ???? implements LogOutInterface {
  ...

  @Override
  public void logout(){
    //your logout procedure
  }
}

3- Have a public method for your WebActivity and allow it to accept LogOutInterface:

public class WebActivity ... {
  private LogOutInterface logoutInterface;
  ...

  public void setLogOut(LogOutInterface logoutInterface) {
     this.logoutInterface = logoutInterface;
  }
}

4- call setLogOut from MainActivity:

public class MainActivity ... {

   public void yourmethod() {
      ...
      webActivity.setLogOut(this);
   }
}

5- call logout function from your WebActivity:

public class WebActivity ... {
   ... 
   public void yourmethod() {
      logoutInterface.logout();
   }
}

hope this helps.

LF00
  • 27,015
  • 29
  • 156
  • 295
Pooya
  • 6,083
  • 3
  • 23
  • 43
  • When I implement your question, I encounter the error, Non-static method 'setLogOut(com.xxx.defaultapplication.LogOutInterface)' cannot be referenced from a static context in the forth step. Same as issue http://stackoverflow.com/questions/4922145/non-static-method-cannot-be-referenced-from-a-static-context-error. And if I implement as the answer http://stackoverflow.com/a/4922186/6521116 I get a java.lang.NullPointerException at WebActivity.yourmethod() – LF00 Nov 15 '16 at 09:18
  • @KrisRoofe note that webActivity is the instance of the class WebActivity not the class itself – Pooya Nov 15 '16 at 14:07
1

Use Application context in your login and logout methods. As they will be managed at Application level.

So change mTencent.logout(MainActivity.this); to mTencent.logout(getApplicationContext());.

Also change your login method to work in application context.

Nitesh Verma
  • 2,460
  • 4
  • 20
  • 36
0

This is a workable one. In the MainActivity, public static Activity thisActivity; & thisActivity = this; then in the WebActivity mTencent.logout(MainActivity.thisActivity);

or just can put the logout function as public static function of MainActivity,

public static void logout() {
    if (mTencent.isSessionValid()) {
        mTencent.logout(thisActivity);
    }
}

then call MainActivity.logout() from WebActivity.

LF00
  • 27,015
  • 29
  • 156
  • 295