我们怎样才能在发布时默认显示活动?

时间:2017-04-29 11:31:05

标签: android android-activity android-manifest launching-application

我制作了一个带导航抽屉的应用,其启动时的默认活动是HomeActivity。我希望UserActivity在启动时默认启动。我尝试用AndroidManifest.xml做它,但它不起作用。你可以告诉我另一种方法,比如在HomeActivity中使用intent或者某些内容,以便在应用程序打开后立即加载到UserActivity吗?

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>

任何帮助都会很棒。谢谢!

3 个答案:

答案 0 :(得分:1)

在HomeActivity的OnCreate中添加 onNavigationItemSelected

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);
    }
  }
} 

从AndroidManifest.xml中删除此行

<activity android:name=".UserActivity"></activity>

答案 1 :(得分:0)

在AndroidManifest中删除此

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

并将其添加到您要在开始时显示的活动

答案 2 :(得分:0)

你好@Bhavesh我以为你正在使用片段所以你不需要在manifest中声明刚刚放在create方法中你将在UserActivity中

getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, new UserActivity())
                .commit();