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