Son Se
Son Se

Reputation: 1

I want to move from MainActivity to another activity using intent, but I don't

Please understand that the code may be complicated. I created a botombar at the bottom of MainActivity and made five different tabs. When you click the Login tab, you try to implement an intent to go to another activity (Login), but it does not work. There was no response when I re-executed the intent declaration in the middle. What's the problem?

MainActivity.java

package com.example.sasohan_main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabReselectListener;

public class MainActivity extends FragmentActivity {
    private ImageButton childbtn;
    private ImageButton senibtn;
    private ImageButton immibtn;
    private ImageButton mmmbtn;
    private Button newpage;
    private ChildFragment child_contact;
    private PregnantFragment pregnant_contact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
        bottomBar.setOnTabReselectListener(new OnTabReselectListener() {
            @Override
            public void onTabReSelected(int tabId) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

                if (tabId == R.id.bottom_Login) {
                    Intent intent = new Intent(MainActivity.this, sasohan_Login.class);
                }
            }
        });

    }
}

Below is a botombar.xml file that exists in the xml folder

MainActivity recognizes botom_Login that exists within botombar.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <tabs>
        <tab
            id="@+id/bottom_child"
            icon="@drawable/bar_child"
            title="child"/>
        <tab
            id="@+id/bottom_parents"
            icon="@drawable/bottom_parents"
            title="senior"/>
        <tab
            id="@+id/bottom_pregnant"
            icon="@drawable/bottom_pregnant"
            title="pregnant"/>
        <tab
            id="@+id/bottom_baseline"
            icon="@drawable/baseline"
            title="disabled"/>
        <tab
            id="@+id/bottom_Login"
            icon="@drawable/baseline_https_black_18dp"
            title="login"/>
    </tabs>
</PreferenceScreen>

Upvotes: 0

Views: 117

Answers (3)

MMG
MMG

Reputation: 3268

if (tabId == R.id.bottom_Login) { 
    Intent intent = new Intent(MainActivity.this, sasohan_Login.class);
    startActivity(intent); 
}

Upvotes: 0

p.mathew13
p.mathew13

Reputation: 921

You forgot to call startActivity. Just creating an intent won't take you to a new activity. You have to call the startActivity method with your intent .Something like this:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
        bottomBar.setOnTabReselectListener(new OnTabReselectListener() {
            @Override
            public void onTabReSelected(int tabId) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

                if (tabId == R.id.bottom_Login) {
                    Intent intent = new Intent(MainActivity.this, sasohan_Login.class);
                    startActivity(intent);
                }
            }
        });

    }

Upvotes: 1

nishagr
nishagr

Reputation: 11

First of All You need to change the function from bottomBar.setOnTabReselectListener to bottomBar.setOnTabSelectListener like I've mentioned below :

package com.example.sasohan_main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabReselectListener;

public class MainActivity extends FragmentActivity {
    private ImageButton childbtn;
    private ImageButton senibtn;
    private ImageButton immibtn;
    private ImageButton mmmbtn;
    private Button newpage;
    private ChildFragment child_contact;
    private PregnantFragment pregnant_contact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(int tabId) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

                if (tabId == R.id.bottom_Login) {
                    Intent intent = new Intent(MainActivity.this, sasohan_Login.class);
                    startActivity(intent)
                }
            }
        });
    }
}

And then as @MMG mentioned add startActivity(intent) in the if statement.

Upvotes: 0

Related Questions