0

I'm building my first interactive android app and trying to find a way to navigate between activities in Android Studio. When the code to switch activities is used, the app shuts down. When the code to navigate to another activity is removed the app doesn't shut down/have bugs.

This is the code i'm using:

package com.example.shashank.login;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class DashboardActivity extends AppCompatActivity {

    String EmailHolder;
    TextView Email;
    Button Next;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);

        Email = findViewById(R.id.textView1);
        Next = findViewById(R.id.button1);

        Intent intent = getIntent();

        // Receiving User Email Send By MainActivity.
        EmailHolder = intent.getStringExtra(MainActivity.UserEmail);

        // Setting up received email to TextView.
        Email.setText(Email.getText().toString() + EmailHolder);


    }


    public void nextActivity(View view) {
        Intent intent = new Intent(DashboardActivity.this, userdashboard.class);
        startActivity(intent);
    }
}

This is the code on the xml page of dashboard activity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_dashboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.shashank.login.DashboardActivity">


    <TextView
        android:text="@string/login_successful"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="#000"
        android:id="@+id/textView1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="176dp" />

    <Button
        android:text="@string/next"
        android:id ="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:onClick="nextActivity"
        android:layout_marginTop="36dp"
        />
</RelativeLayout>

Essentially trying to go to userdashboard from DashboardActivity. It should navigate from DashboardActivity to userdashboard class, but the app shuts down.

What's wrong with this code? How do I navigate between activities without the app closing?

acorn
  • 11
  • 2
  • do you got any error/warning in the logcat/run panel in Android Studio? If you got a build fails, you should first resolve all issues. – user2342558 Jun 09 '23 at 15:01

1 Answers1

2

If you want to navigate between two activity then you can use the below code.

public class DashboardActivity extends AppCompatActivity {

TextView Email;
Button Next;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Email = findViewById(R.id.textView1);
    Next = findViewById(R.id.button1);
    
    // Here i have created click listener so when you tap on the next button that will invoke this click listener and inside it we are calling the nextActivity function to open new activity.
    Next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            nextActivity();
        }
    });
}

public void nextActivity() {
    // Here i passed two activity first is the current activity and second is the name of the activity which you want to navigate from the current activity.
    Intent intent = new Intent(DashboardActivity.this, NextActivity.class);
    startActivity(intent);
}

}

You can use this code put the button click listener to invoke the navigation from one activity to another.

Khush Parmar
  • 316
  • 2
  • 9