在開(kāi)發(fā)過(guò)程中是否遇到這么一個(gè)問(wèn)題,功能描述:點(diǎn)擊后執(zhí)行一段代碼繁堡,但是要在點(diǎn)擊的時(shí)候檢測(cè)是否已登錄瞎饲。這個(gè)功能很簡(jiǎn)單口叙,但登錄后要繼續(xù)執(zhí)行剛才那段代碼呢?是不是有點(diǎn)無(wú)頭緒了嗅战。在Java中可以用接口去回調(diào)妄田,但觸發(fā)的時(shí)間又是在登錄完成后俺亮。怎么辦?
其實(shí)對(duì)LifecycleObserver有了解的疟呐,大概都有思路了脚曾。LifecycleObserver是對(duì)Activity和Fragment的生命周期進(jìn)行監(jiān)聽(tīng),以便在特定的生命周期內(nèi)作出相關(guān)操作启具。思考下本讥,我們點(diǎn)擊后取到登錄界面,登錄回來(lái)的Activity是不是走了onResume呢鲁冯,那么登錄成功后拷沸,再走后續(xù)的方法:
public class LoginIntercept implements LifecycleObserver {
private BlockIntentFun blockIntentFun;
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void resume(){
if (BaseApplication.getInstance().isLogin){
if (blockIntentFun != null){
blockIntentFun.execute();
}
blockIntentFun = null;
}
}
public void setBlockIntentFun(BlockIntentFun blockIntentFun) {
this.blockIntentFun = blockIntentFun;
}
public interface BlockIntentFun{
void execute();
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) 的意思是在onResume的時(shí)候執(zhí)行下面方法,方法名可以隨便定晓褪;因?yàn)槊看蝟nResume的時(shí)候都會(huì)走這個(gè)方法堵漱,所以執(zhí)行完要置空,不然每次都會(huì)被調(diào)用涣仿。
在Activity中勤庐,需要注冊(cè)生命周期的監(jiān)聽(tīng),分別在onStart和onDestroy加上監(jiān)聽(tīng)和移除監(jiān)聽(tīng)好港。
public class InterceptActivity extends AppCompatActivity {
private LoginIntercept intercept;
private TextView textView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intercept_activity);
intercept = new LoginIntercept();
textView = findViewById(R.id.text);
textView.setOnClickListener(v -> loginCheck());
}
@Override
protected void onStart() {
super.onStart();
getLifecycle().addObserver(intercept);
}
@Override
protected void onDestroy() {
super.onDestroy();
getLifecycle().removeObserver(intercept);
}
private void doSomething(){
//點(diǎn)擊后要做的事
}
private void loginCheck(){
//需要獲取登錄狀態(tài)愉镰,按自己需求設(shè)定
if (BaseApplication.getInstance().isLogin){
doSomething();
}else{
intercept.setBlockIntentFun(this::doSomething);
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
//這里假設(shè)登錄成功了,實(shí)際操作按自己的邏輯來(lái)處理
BaseApplication.getInstance().isLogin = true;
}
}
}
這樣就解決了钧汹,每次只在點(diǎn)擊的時(shí)候調(diào)用了丈探;假如用Kotlin的話,會(huì)方便很多拔莱,前提是對(duì)Kotlin的高階函數(shù)有了解
class LoginInterceptKt : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun resume(){
if (BaseApplication.getInstance().isLogin){
blockIntercept?.invoke()
}
blockIntercept = null
}
}
private var blockIntercept: (() -> Unit)? = null
fun loginCheck(activityKt: InterceptActivityKt,block:(() -> Unit)?){
if (BaseApplication.getInstance().isLogin){
//如果登錄了就不攔截碗降,直接執(zhí)行
block?.invoke()
}else{
activityKt.startActivity(Intent(activityKt,LoginActivity::class.java))
//這里假設(shè)登錄成功了,實(shí)際按自己需求來(lái)處理
blockIntercept = block
}
}
用Kotlin的方式寫(xiě)看起來(lái)更簡(jiǎn)潔點(diǎn)塘秦,用高階函數(shù)取代了回調(diào)的功能讼渊。
class InterceptActivityKt : AppCompatActivity() {
private val intercept by lazy { LoginInterceptKt() }
override fun onStart() {
super.onStart()
lifecycle.addObserver(intercept)
}
override fun onDestroy() {
super.onDestroy()
lifecycle.removeObserver(intercept)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.intercept_activity)
text.setOnClickListener {
loginCheck(this) {
doSomething()
}
}
}
private fun doSomething(){
//點(diǎn)擊后要做的事
}
}
解決這個(gè)問(wèn)題的方法可能有多種,但用這個(gè)方法解決的話尊剔,要對(duì)LifecycleObserver有了解爪幻。