Skip to content

Instantly share code, notes, and snippets.

@hello2pal
Last active January 25, 2023 13:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hello2pal/4548094 to your computer and use it in GitHub Desktop.
Save hello2pal/4548094 to your computer and use it in GitHub Desktop.
How to Navigate from One Activity to Another Activity in Android
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button; //
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
*Each activity must have a corresponding layout XML file
*/
setContentView(R.layout.activity_main);// activity_main is corresponding XML file
/*
* Initializing button XML button id. findViewById is a method which
* helps to initialize with particular id. btn_go_to_another_activity is
* a button name which I have given in XML file
*/
button = (Button) findViewById(R.id.btn_go_to_another_activity);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*
* Intent is just like glue which helps to navigate one activity
* to another.
*/Intent intent = new Intent(MainActivity.this,
AnotherActivity.class);
startActivity(intent); // startActivity allow you to move
}
});
}
}
@Muhammdnadeem
Copy link

how to connect two activities?
I tried to connect two activity but app crashed please help me

@vetonhasani
Copy link

Fragment fr;

if (view == findViewById(R.id.buttonId))
fr = new FragmentActivityOne();
else
fr = new FragmentActivityTwo();

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment, fr);
transaction.commit();

@israr7933
Copy link

Can anyone please help to find out error after login succesfully they dont navigate to another activity

package com.Dimple.flashchatnewfirebase;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class LoginActivity extends AppCompatActivity {

// TODO: Add member variables here:
private FirebaseAuth mAuth;
// UI references.

private AutoCompleteTextView mEmailView;
private EditText mPasswordView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    mEmailView = (AutoCompleteTextView) findViewById(R.id.login_email);
    mPasswordView = (EditText) findViewById(R.id.login_password);

    mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
            if (id == R.integer.login || id == EditorInfo.IME_NULL) {
                attemptLogin();
                return true;
            }
            return false;
        }
    });

    // TODO: Grab an instance of FirebaseAuth
    mAuth = FirebaseAuth.getInstance();

}

// Executed when Sign in button pressed
public void signInExistingUser(View v)   {
    // TODO: Call attemptLogin() here
    attemptLogin();

}

// Executed when Register button pressed
public void registerNewUser(View v) {
    Intent intent = new Intent(this, com.Dimple.flashchatnewfirebase.RegisterActivity.class);
    finish();
    startActivity(intent);
}

// TODO: Complete the attemptLogin() method

private void attemptLogin() {
String email = mEmailView.getText().toString();
String password = mPasswordView.getText().toString();

    if (email.equals("") || password.equals("")) return;
    Toast.makeText(this, "Login in progress...", Toast.LENGTH_SHORT).show();


    // TODO: Use FirebaseAuth to sign in with email & password
  mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
      @Override
      public void onComplete(@NonNull Task<AuthResult> task) {
          Log.d("FlashChat", "signInWithEmail() onComplete: " + task.isSuccessful());
         if (!task.isSuccessful()) {
              Log.d("FlashChat", "Problem signing in:" + task.getException());
              showErrorDialog("There was a problem in signing in");

         } else {
              Intent intent = new Intent(LoginActivity.this, MainChatActivity.class);
            finish();
            startActivity(intent);
         }
      }
  });


}

   // TODO: Show error on screen with an alert dialog
private void showErrorDialog(String message){
   new AlertDialog.Builder(this)
         .setTitle("Oops")
           .setMessage(message)
         .setPositiveButton(android.R.string.ok,null)
          .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
}

}

@tamojit-123
Copy link

TextView textView = (TextView) findViewById(R.id.homeText);
textView.setOnClickListener(new View.OnContextClickListener()

{
    @Override
    public void onclick (View v) {
        Intent intent = new Intent(RegisterActivity.this, ForgetPassword.class);
    startActivity(intent);
    }

`});```

Copy link

ghost commented Jan 13, 2020

@chaufferservicezurich
Copy link

Noble Transfer is an online private transfer and shuttle services offering reliable, luxury and comfortable transfer services from & to the airport and tourist resorts.limousine service zurich

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment