Android 登錄成功后再跳轉(zhuǎn)到目標頁(三)

介紹

前邊兩篇介紹兩種方案都是基于Android基礎(chǔ)知識實現(xiàn)的,其中方案二中的缺點是:工具類要維護所有的跳轉(zhuǎn)和數(shù)據(jù)傳遞,這里邊包含F(xiàn)ragment啟動Activity的方式,而且還要管理所有的邏輯,當跳轉(zhuǎn)完成后要記得從內(nèi)存中移出去。但是當沒有登錄的時候跳轉(zhuǎn)到登錄頁面,用戶沒有進行登錄而是按下返回鍵,那么剛才添加的跳轉(zhuǎn)和數(shù)據(jù)就沒有去向和消費了,就會一直滯留在緩存中了。一般情況用戶也不會累計太多這種操作的。當然針對這種情況也有處理方案:當退出APP的時候清除緩存,或者使用LRU緩存,當內(nèi)存滿的時候,清除掉那些舊的無用的數(shù)據(jù)。

那么還有沒有更好點的方案呢?

當然有了,我們學習的AOP思想就可以解決這個問題,關(guān)于AOP的介紹不是本文的重點,網(wǎng)上有很多不錯博文,請大家自行解決。
基于AOP的思想我們使用動態(tài)代理的思想處理登錄成功后再跳轉(zhuǎn)到目標頁的邏輯。

直接跳轉(zhuǎn).png
切面.png

面向切面的思想很簡單如上圖,我們在所有橫向跳轉(zhuǎn)的頁面中間插一個縱向的切面,進行攔截處理,當沒有登錄的時候,進行登錄。

還和第二篇前面一樣,這里不在講述不傳遞數(shù)據(jù)的情況,直接講述傳遞數(shù)據(jù)的情況。

定義動態(tài)代理需要的接口

public interface ILaunchManagerService {
    public void startActivity(Intent intent);
}

這里ILaunchManagerService命名偷學的ActivityManagerService的方式,里邊只是定義一個startActivity抽象方法。

定義InvocationHandler的實現(xiàn)類

public class LaunchInvocationHandler implements InvocationHandler {

    private ILaunchManagerService launchManagerService;
    private Context context;

    public LaunchInvocationHandler(Context context, ILaunchManagerService launchManagerService) {
        this.context = context;
        this.launchManagerService = launchManagerService;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (!Constant.isLogin) {
            Intent intent = (Intent) args[0];
            ComponentName component = intent.getComponent();
            String className = component.getClassName();
            Bundle bundleExtra = intent.getBundleExtra(Constant.KEY_DATA);
            bundleExtra.putString(Constant.KEY_CLASS_NAME, className);
            intent.putExtra(Constant.KEY_DATA, bundleExtra);
            intent.setClass(context, LoginActivity.class);
            args[0] = intent;
        }
        method.invoke(launchManagerService, args);

        return null;
    }
}

invoke方法內(nèi)部判斷沒有登錄的時候,修改實現(xiàn)方法跳轉(zhuǎn)到登錄頁,并且把目標頁的className傳遞給登錄頁。

使用

MainActivity.java

public class MainActivity extends AppCompatActivity implements ILaunchManagerService {
    private Button myGoodsBtn, myFavoritesBtn;
    private ILaunchManagerService launchManagerService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myGoodsBtn = findViewById(R.id.myGoodsBtn);
        myFavoritesBtn = findViewById(R.id.myFavoriteBtn);

        launchManagerService = (ILaunchManagerService) Proxy.newProxyInstance(this.getClassLoader(), new Class[]{ILaunchManagerService.class}, new LaunchInvocationHandler(this,this));

        myGoodsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lookMyGoods();
            }
        });

        myFavoritesBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lookMyFavorite();
            }
        });
    }

    private void lookMyGoods() {
        Intent intent = new Intent(this, MyGoodsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(Constant.KEY_MSG, "通過登錄傳遞的產(chǎn)品");
        intent.putExtra(Constant.KEY_DATA, bundle);
        launchManagerService.startActivity(intent);
    }

    private void lookMyFavorite() {
        Intent intent = new Intent(this, MyGoodsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(Constant.KEY_MSG, "通過登錄傳遞的收藏");
        intent.putExtra(Constant.KEY_DATA, bundle);
        launchManagerService.startActivity(intent);
    }

}

MainActivity 采用類適配器的方式,使用代理對象實現(xiàn)頁面的跳轉(zhuǎn)。launchManagerService.startActivity,會調(diào)用到代理類的invoke的方法,進行切面攔截處理。

登錄頁


public class LoginActivity extends AppCompatActivity {
    ......
    private void handleLoginSuccess3(){
        String className = bundle.getString(Constant.KEY_CLASS_NAME);
        if (TextUtils.isEmpty(className)) {
            return;
        }

        Intent intent = new Intent();
        intent.setComponent(new ComponentName(this, className));
        bundle.remove(Constant.KEY_CLASS_NAME);
        intent.putExtra(Constant.KEY_DATA, bundle);

        startActivity(intent);
        finish();
    }
}

登錄成功后根據(jù)接收的目標頁 className 進行頁面跳轉(zhuǎn)

商品和收藏頁面

public class MyFavoriteActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorite);


        Bundle bundle = getIntent().getBundleExtra(Constant.KEY_DATA);
        if (bundle != null) {
            String msg = bundle.getString(Constant.KEY_MSG);
            if (!TextUtils.isEmpty(msg)) {
                Toast.makeText(this, "我的收藏--》msg="+msg,Toast.LENGTH_LONG).show();
            }
        }
    }

}

public class MyGoodsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);

        Bundle bundle = getIntent().getBundleExtra(Constant.KEY_DATA);
        if (bundle != null) {
            String msg = bundle.getString(Constant.KEY_MSG);
            if (!TextUtils.isEmpty(msg)) {
                Toast.makeText(this, "我的產(chǎn)品--》msg="+msg,Toast.LENGTH_LONG).show();
            }
        }
    }

}

這兩個頁面就是正常的接收數(shù)據(jù)進行處理,不再詳述。

總結(jié)

目前來說這種方案是最簡單,容易維護的方案。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容