简体   繁体   English

如何从两个活动启动一个 android 应用程序

[英]How to start an android app from two activity

I am developing an android app in which I am signing up with my mobile number on splash screen .我正在开发一个 android 应用程序,我在启动屏幕上用我的手机号码注册。 What I want is that signing up should be neccessary only once and first time at the time of app installation.我想要的是,在安装应用程序时只需要一次和第一次注册。 After installing the app, app should open from another activity , not the from splash screen.安装应用程序后,应用程序应该从另一个活动打开,而不是从初始屏幕。 how it is possible.怎么可能。

On successful signup of login you have to store data in SharedPreferences .成功注册登录后,您必须将数据存储在SharedPreferences

AppTypeDetails is class for SharedPreferences. AppTypeDetails 是 SharedPreferences 的类。

 AppTypeDetails.getInstance(SignUpActivity.this).setEmail(<Your Email ID>);
 AppTypeDetails.getInstance(SignUpActivity.this).setPassword(<Your Password>);

AppTypeDetails.java AppTypeDetails.java

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class AppTypeDetails {

    private SharedPreferences sh;

    private AppTypeDetails() {

    }

    private AppTypeDetails(Context mContext) {
        sh = PreferenceManager.getDefaultSharedPreferences(mContext);
    }

    private static AppTypeDetails instance = null;

    /**
     * 
     * @param mContext
     * @return {@link AppTypeDetails}
     */
    public synchronized static AppTypeDetails getInstance(Context mContext) {
        if (instance == null) {
            instance = new AppTypeDetails(mContext);
        }
        return instance;
    }

    // get username
    public String getEmail() {
        return sh.getString("email", "");
    }

    public void setEmail(String email) {
        sh.edit().putString("email", email).commit();
    }

    // get password
    public String getPassword() {
        return sh.getString("password", "");
    }

    public void setPassword(String password) {
        sh.edit().putString("password", password).commit();
    }

    public void clear() {
        sh.edit().clear().commit();
    }

}

Now check below code in splash screen.现在在启动画面中检查以下代码。

String email = AppTypeDetails.getInstance(SplashScreen.this).getEmail();
String pass = AppTypeDetails.getInstance(SplashScreen.this).getPassword();

if (email.trim().isEmpty() && pass.trim().isEmpty()) {
    Intent intent = new Intent(SplashScreen.this, Login.class);
    startActivity(intent);
} else {
    Intent intent = new Intent(SplashScreen.this, MainScreenTabHost.class);
    startActivity(intent);
}

For clear SharedPreferences :对于明确的SharedPreferences

Call clear() method on logout.注销时调用 clear() 方法。

In splash class you should check if the app runs for the first time or not.在启动类中,您应该检查应用程序是否第一次运行。 If yes so continue, if not, start the second activity.如果是,则继续,如果不是,则开始第二个活动。 It is possible by checking a boolean, store it in shared preferences and every time check its value at startup.可以通过检查布尔值,将其存储在共享首选项中并每次在启动时检查其值。

Try to use SharedPrefrence.But it is not the complete answer.Show your SplashScreen check in that your user is register or not.尝试使用 SharedPrefence。但这不是完整的答案。显示您的 SplashScreen 检查您的用户是否注册。

       Boolean REG_RESPONCE = new   Session_manag(getActivity()).IsSessionCheckOrCreated();
            if (REG_RESPONCE.equals(true)) {
                Intent toHomeactivity = new Intent(Splash.this, MainMenu.class);
                finish();
                startActivity(toHomeactivity);
            } else {

                Intent i = new Intent(Splash.this, SignUp.class);
                finish();
                startActivity(i);
            }

This is too easy to handle.这太容易处理了。 What you need is a concept to how to implement this.您需要的是如何实现这一点的概念。 So I am giving you the road map , do some searching on the internet about the things I am going to tell you.所以我给你路线图,在互联网上搜索一些我要告诉你的事情。 These steps are as under这些步骤如下

  1. I think the easy way of doing this , is to implement the shared preferences.我认为这样做的简单方法是实现共享首选项。 what shared preferences do ?什么共享偏好呢? They will store the information of your client into the app such as his name , password他们会将您客户的信息存储到应用程序中,例如他的姓名密码
  2. So when the Spalsh start first check if there are values in the shared preferences , if there are values then its mean that your user has already signed in.因此,当 Spalsh 开始时首先检查共享首选项中是否有值,如果有值则表示您的用户已经登录。
  3. In case of there is no value stored by the user then its mean that you need to take him to the signup activity.如果用户没有存储任何值,则意味着您需要将他带到注册活动。
  4. So in this way you can take your user where you want.因此,通过这种方式,您可以将用户带到您想要的地方。 But there is no way to use two activities at the same time.但是没有办法同时使用两个活动。

You can read shared preferences from here and this is a nice tutorial to get you started.您可以从这里阅读共享首选项, 是一个很好的入门教程。

You can check condition on splash screen that user has already signup with number or not.for that you have to save that number in SharedPreferences.您可以在初始屏幕上检查用户是否已使用号码注册的条件。为此,您必须将该号码保存在 SharedPreferences 中。 Follow thew below steps,请按照以下步骤操作,

Step 1 : When user open app first time splash screen will come.here you can check condition for number.At first time, when user come on app value(number) dosn't exist in SharedPreferences.so app will ask for enter number.第 1 步:当用户第一次打开应用时会出现闪屏。在这里你可以检查数字的条件。第一次,当用户打开应用时,SharedPreferences.so 中不存在应用值(数字)。所以应用会要求输入数字。 when user enter number and submit, store that in SharedPreferences.当用户输入数字并提交时,将其存储在 SharedPreferences 中。

Step 2 : now, Second time when user come on splash screen, that condition become true because SharedPreferences has the Value(number).第 2 步:现在,当用户第二次出现在启动画面时,该条件变为真,因为 SharedPreferences 具有 Value(number)。 so you can redirect app on second activity.这样您就可以在第二个活动中重定向应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM