LF-DevJourney
LF-DevJourney

Reputation: 28529

How to access another activity?

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)

Upvotes: 0

Views: 1344

Answers (4)

LF-DevJourney
LF-DevJourney

Reputation: 28529

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.

Upvotes: 0

Pooya
Pooya

Reputation: 6126

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.

Upvotes: 1

Nitesh Verma
Nitesh Verma

Reputation: 2500

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.

Upvotes: 1

Victor
Victor

Reputation: 4199

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 */};

Upvotes: 1

Related Questions