How to access another activity?

自作多情 提交于 2019-12-08 09:39:23

问题


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)


回答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.




回答2:


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



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/40601068/how-to-access-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!