android - How to get an instance of an activity from a fragment in Android?
问题描述
I have a SignInActivity like this:
public class SignInActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
signInButton.setOnClickListener(v -> signIn());
noAccountButton.setOnClickListener(v -> goToMainActivity());
}
private void signIn() {
//Start auth process
}
}
My app can be used with or without an account. In both situations the user is redirected to MainActivity. This activity holds a fragment where I display a message such if the users wants to become a user of the app though. I also have a button like this:
public class UserFragment extends Fragment {
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
signInButton.setOnClickListener(v -> {
instanceOfSingInActivity.signIn();
// ^ ^
});
}
}
And I need an instance of the SignInActivity so I can call the signIn() method. How can I solve this?
解决方案
推荐阅读
- r - py_call_impl 中的错误(可调用,dots$args,dots$keywords)
- c# - 有时显示 ArgumentOutOfException 错误
- javascript - iOS 12 Safari:由于访问控制检查,无法加载 XMLHttpRequest
- react-native - 使用 fetch() 获取承诺响应
- pytorch - 为什么我的 RL 模型在加载到 pytorch 后表现不一样?
- ios - ios:“fr”标识符的垃圾区域设置
- asp.net - azure 连接字符串格式 - 在 azure 中添加
- css - 为 tr 和 td 设置不同的高度
- wordpress - wordpress 中的 get_term_by 名称不返回包含“&”的术语
- windows - 运行docker时偶尔长时间停顿