I have total 9 activity and I want to use Up Navigation Button.I have used 2 parent activity i.e One for Login activity and another for Option activity. Please see the code.
Its working for: ResetActivity -> Loginactivity; ForgotPasswordActivity -> Loginactivity
Rest of them is not working Pl help me to solve the issue.
<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();
}
}
I assume you click the Register button from the LoginActivity right? In the btnLinkToRegister of LoginActivity, you are calling finish(); after starting the RegisterActivity. The finish() method closes the activity, thus, when you are press the up button (or press the back button), the LoginActivity cannot be opened anymore, so it will go to the homescreen.