如何在不使用Android应用程序的刷新和重新创建的情况下重新启动应用程序?

时间:2018-09-04 10:07:37

标签: java android

在我的应用程序中,我有一个登录和注册部分,当用户注册应用程序将其号码保存到数据库中时,然后在主应用程序中,它将使用该电话从服务器获取用户信息。我的问题是,当我按下退出按钮时,应用程序会执行此操作,但问题仍然存在于购物车和其他需要登录的部件,仍可以正常工作,而当我重新启动应用程序时,它将完全注销用户并让用户唱歌-再次进入。我用过recreate & finish,但无法正常工作。这是检查用户是否存在的代码,该make应用程序的哪个库用于获取用户信息:

private void checkSessionLogin() {

    UserDbHelper userDbHelper = new UserDbHelper(getApplicationContext());
    SQLiteDatabase database = userDbHelper.getReadableDatabase();

    Cursor cursor = userDbHelper.readPhone(database);


    while (cursor.moveToNext())
    {
        PhoneNmber = cursor.getString(cursor.getColumnIndex(UserDatabaseUser.UserEntry.PHONE));

    }

    if ((AtlasPref.getInt("user_id",1) == 1)){


        swipeRefreshLayout.setRefreshing(true);
        //Check exists user on Serve (MySQL)
        mService.checkExistsUser(PhoneNmber)
            .enqueue(new Callback<CheckUserResponse>() {
                @Override
                public void onResponse(Call<CheckUserResponse> call, Response<CheckUserResponse> response) {
                    CheckUserResponse userResponse = response.body();
                    if (userResponse.isExists())
                    {
                        //Request information of current user
                        mService.getUserInformation(PhoneNmber)
                                .enqueue(new Callback<User>() {
                                    @Override
                                    public void onResponse(Call<User> call, Response<User> response) {
                                        Common.currentUser = response.body();
                                        if (Common.currentUser != null) {
                                            swipeRefreshLayout.setRefreshing(false);

                                            updateTokenToServer();

                                            //Set Info
                                            txt_name.setText(Common.currentUser.getName());
                                            txt_phone.setText(Common.currentUser.getPhone());
                                            hideItem();
                                            //Set Avatar
                                            if (!TextUtils.isEmpty(Common.currentUser.getAvatarUrl())) {
                                                Picasso.with(getBaseContext())
                                                        .load(new StringBuilder(Common.BASE_URL)
                                                                .append("user_avatar/")
                                                                .append(Common.currentUser.getAvatarUrl()).toString())
                                                        .into(img_avatar);
                                            }

                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<User> call, Throwable t) {
                                        swipeRefreshLayout.setRefreshing(false);
                                        Log.d("ERROR",t.getMessage());

                                    }
                                });
                    }
                }

                @Override
                public void onFailure(Call<CheckUserResponse> call, Throwable t) {

                }
            });
}
else if ((AtlasPref.getInt("user_id",0) == 0))

    {
        showItem();

        txt_name.setText("Name");
        txt_phone.setText("Number");
    }
    else
    {

        showItem();

        txt_name.setText("Name");
        txt_phone.setText("Number");
    }

}

退出按钮:

builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                UserDbHelper userDbHelper = new UserDbHelper(getApplicationContext());
                SQLiteDatabase database = userDbHelper.getReadableDatabase();

                userDbHelper.deleteContact(PhoneNmber,database);
                userDbHelper.close();

                //Clear All Activity
                SharedPreferences.Editor editor=AtlasPref.edit();
                editor.putInt("user_id",0);

                editor.commit();
                // first- i've tried this without 2nd, 3rd
                Intent intent = getIntent();
                finish();
                startActivity(intent);
                // 2nd - I've tried this without 1st, 3rd
                recreate();
                // 3rd - I've tried this without 2nd, 3rd
                finish();

            }
        });

3 个答案:

答案 0 :(得分:1)

用户注销后,从头开始重新启动整个应用程序。

Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

在这里您必须正确清除共享首选项

SharedPreferences.Editor editor = AtlasPref.edit();
editor.clear();
editor.commit();
finish();

答案 1 :(得分:1)

我在这里建立图书馆

https://github.com/ksc91u/apprestart

  1. 按照以下说明添加jitpack:https://jitpack.io/#ksc91u/apprestart

  2. 在您的Application.onCreate()中 override fun onCreate() { super.onCreate() AppPhoenix.init(this) }

  3. 在您的活动中,“ this”是活动 override fun onCreate() { super.onCreate() AppPhoenix.init(this) }

答案 2 :(得分:0)

感谢您的回复。

对我有用的解决方案是使用

System.exit(0);