Android操作栏向上导航如何SetParentActivity?这对我不起作用



我总共有9个活动,我想使用向上导航按钮。我使用了2个父活动,即一个用于登录活动,另一个用于选项活动。请查看代码

它适用于:ResetActivity -> Loginactivity;ForgotPasswordActivity ->登录活动

其余的不工作,请帮我解决这个问题。

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".LoginActivity"
        android:label=" Login"
        android:launchMode="singleTop" >
    </activity>
    <activity
        android:name=".RegisterActivity"
        android:label="Register"
        android:parentActivityName=".LoginActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".LoginActivity" />
    </activity>
    <activity
        android:name=".ForgotPasswordActivity"
        android:label="Forgot password"
        android:parentActivityName=".LoginActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".LoginActivity" />
    </activity>
    <activity
        android:name=".ResetActivity"
        android:label="Reseting password"
        android:parentActivityName=".LoginActivity" >
    </activity>
    <activity
        android:name=".OptionActivity"
        android:label="Option details"
        android:launchMode="singleTop" >
    </activity>
    <activity
        android:name=".ProductActivity"
        android:label="Add Product"
        android:parentActivityName=".OptionActivity" >
    </activity>
    <activity
        android:name=".MapsActivity"
        android:label=":location"
        android:parentActivityName=".OptionActivity" >
    </activity>
    <activity
        android:name=".HelpActivity"
        android:label="How to use App"
        android:parentActivityName=".OptionActivity" >
    </activity>
    <activity
        android:name=".ViewActivity"
        android:label="View"
        android:parentActivityName=".OptionActivity" >
    </activity>

My LOGIN ACTIVITY CODE:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    Button btnLinkToRegister = (Button) findViewById(R.id.btnlinkregister);
    Button btnToForgot = (Button) findViewById(R.id.btnforgot);
    Button signIn = (Button) findViewById(R.id.signin);
    inputUsername = (EditText) findViewById(R.id.name);
    inputPassword = (EditText) findViewById(R.id.password);

    // Progress dialog
    pDialog = new ProgressDialog(this);
    pDialog.setCancelable(false);

    // SqLite database handler
    db = new UserData(getApplicationContext());

    btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent i = new Intent(LoginActivity.this,
                    RegisterActivity.class);
            startActivity(i);
            finish();
        }
    });

    btnToForgot.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent forgot = new Intent(LoginActivity.this,
                    ForgotPasswordActivity.class);
            startActivity(forgot);
        }
    });

    // Login button Click Event
    signIn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String name = inputUsername.getText().toString();
            String password = inputPassword.getText().toString();
            if(name.equals("")&&password.equals("")) {
                Toast.makeText(getApplicationContext(), "Username or Password is empty",
                        Toast.LENGTH_LONG).show();
                return;
            }
            try {

                if (name.length() > 0 && password.length() > 0) {
                    db = new UserData(LoginActivity.this); //create again database to retrive
                    if( db.Login(name, password)){
                        Toast.makeText(getApplicationContext(), "Login is Successful", Toast.LENGTH_LONG).show();
                        Intent work = new Intent(LoginActivity.this,
                                OptionActivity.class);
                      // work.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                       //work.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(work);
                        finish();
                    } else {
                        Toast.makeText(getApplicationContext(), "Invalid Username/Password", Toast.LENGTH_LONG).show();
                    }

                }
            } catch (Exception e) {
                Toast.makeText(LoginActivity.this, "Problem occurred",
                        Toast.LENGTH_LONG).show();
            }
        }
    });}

private void showDialog() {
    if (!pDialog.isShowing())
        pDialog.show();
}
private void hideDialog() {
    if (pDialog.isShowing())
        pDialog.dismiss();
}
@Override
public void onBackPressed() {
    super.onBackPressed();
}

}

My REGISTER ACTIVITY CODE:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    inputUsername = (EditText) findViewById(R.id.name);
    inputPassword = (EditText) findViewById(R.id.password);
    inputEmail = (EditText) findViewById(R.id.email);
    register = (Button) findViewById(R.id.btnRegister);
    btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
    ActionBar ab = getActionBar();
    ab.setDisplayHomeAsUpEnabled(true);

    // Progress dialog
    pDialog = new ProgressDialog(this);
    pDialog.setCancelable(false);
    // SQLite database handler
    db = new UserData(getApplicationContext());

    //When register button is click the database updated
    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = inputUsername.getText().toString();
            String password = inputPassword.getText().toString();
            String email = inputEmail.getText().toString();
            if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
                //inputUsername.setText("");
                //inputPassword.setText("");
                //inputEmail.setText("");
                //registerUser(name, email, password);
                db.addUser(name, email, password);
                Toast.makeText(getBaseContext(), "Registration is successful", Toast.LENGTH_LONG).show();
                Intent i1 = new Intent(getApplicationContext(),
                        LoginActivity.class);
                startActivity(i1);

            }else{
                Toast.makeText(getApplicationContext(),
                        "Please enter your details!", Toast.LENGTH_LONG)
                        .show();
            }
        }
    });
    // Link to Login Screen
    btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    LoginActivity.class);
            startActivity(i);
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

private void showDialog() {
    if (!pDialog.isShowing())
        pDialog.show();
}
private void hideDialog() {
    if (pDialog.isShowing())
        pDialog.dismiss();
}
@Override
public void onBackPressed() {
    Intent reg = new Intent(RegisterActivity.this,
            LoginActivity.class);
    startActivity(reg);
    finish();
}

}

我假设你从LoginActivity点击注册按钮对吗?在LoginActivity的btnLinkToRegister中,在启动RegisterActivity后调用finish();finish()方法关闭活动,因此,当你按下向上按钮(或按下后退按钮)时,LoginActivity不能再打开,因此它将转到主屏幕

相关内容

  • 没有找到相关文章

最新更新