0

I made an app with navigation drawer whose default activity at startup is HomeActivity. I want it to the UserActivity to start by default at launch. I tried doing it with AndroidManifest.xml but it didn't work. Can u tell me another way of doing it like using intent or something in the HomeActivity such that it loads to UserActivity as soon as the app opens?

HomeActivity.java

package thenerdimite.nuttybuddies;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;

public class HomeActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        android.app.FragmentManager fragmentmanager = getFragmentManager();
        if (id == R.id.nav_activity_user) {
            fragmentmanager.beginTransaction()
                    .replace(R.id.content_frame
                            , new UserActivity())
                    .commit ();
        } else if (id == R.id.nav_activity_blog) {
            fragmentmanager.beginTransaction()
                    .replace(R.id.content_frame
                            , new BlogActivity())
                    .commit ();
        } else if (id == R.id.nav_activity_Chat) {
            fragmentmanager.beginTransaction()
                    .replace(R.id.content_frame
                            , new ChatActivity())
                    .commit ();
        } else if (id == R.id.nav_activity_fbgroup) {
            fragmentmanager.beginTransaction()
                    .replace(R.id.content_frame
                            , new FBGroupActivity())
                    .commit ();

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".HomeActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".BlogActivity" />
        <activity android:name=".ChatActivity" />
        <activity android:name=".FBGroupActivity" />
        <activity android:name=".UserActivity"></activity>
    </application>

</manifest>

UserActivity.java

package thenerdimite.nuttybuddies;

import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class UserActivity extends Fragment {

    View myView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.activity_user, container, false);
        return myView;
    }
}

activity_home_drawer.xml

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

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_activity_user"
            android:icon="@drawable/ic_menu_camera"
            android:title="Home" />
        <item
            android:id="@+id/nav_activity_blog"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Blog" />
        <item
            android:id="@+id/nav_activity_Chat"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Chat Room" />
        <item
            android:id="@+id/nav_activity_fbgroup"
            android:icon="@drawable/ic_menu_manage"
            android:title="Facebook Group" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="About Us" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Click Here to Report App related Issues" />
        </menu>
    </item>

</menu>

Any help would be great. Thank You!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Bhavesh
  • 139
  • 6
  • The correct way to do that should be in AndroidManifest, can you show us how you tried to do that and didn't work? – Daniel Amarante Apr 29 '17 at 11:37
  • UserActivity is your fragment, not an activity, So try to load UserActivity Fragment OnCreate of HomeActivity.java. – Janak Apr 29 '17 at 11:40

3 Answers3

1

Add onNavigationItemSelected in OnCreate of HomeActivity.

public class HomeActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

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

 ......


 ......

 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
 navigationView.setNavigationItemSelectedListener(this);

 if (savedInstanceState == null) {
          MenuItem item =  navigationView.getMenu().getItem(0);
          onNavigationItemSelected(item);
    }
  }
} 

And remove this line From AndroidManifest.xml

<activity android:name=".UserActivity"></activity>
Janak
  • 607
  • 5
  • 23
  • if i remove from the androidmanifest file an error comes Error:The element type "application" must be terminated by the matching end-tag "". Error:Exception while parsing the supplied manifest file G:\BHAVESH'S DOCUMENTS\Android Studio App Development\NuttyBuddies\app\src\main\AndroidManifest.xml > The element type "application" must be terminated by the matching end-tag "". – Bhavesh Apr 29 '17 at 12:20
  • @Bhavesh The element type "application" must be terminated by the matching end-tag ". Add "" above . – Janak Apr 29 '17 at 12:22
  • Oh got it it was not the problem of application tag but the problem was that i left the unused activity tag as it. – Bhavesh Apr 29 '17 at 12:23
  • @Bhavesh Ok. Clean and Build your project – Janak Apr 29 '17 at 12:27
0

In AndroidManifest delete this

<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>

And add it to the activity you want to show at start

iGio90
  • 290
  • 1
  • 4
  • 15
0

Hello @Bhavesh I thought you are using fragment so you doesn't need to declare in manifest just put last in on create method you will be in UserActivity

getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, new UserActivity())
                .commit();
Piyush Patel
  • 371
  • 1
  • 5
  • 13