Android:点击“最近的按钮”时,启动新活动而不显示两项活动

时间:2017-03-22 01:15:36

标签: android android-fragments android-activity

我正在使用此代码在我参加活动时开始新活动:

Intent open = new Intent(context.getActivity(), ActivitySecond.class);
context.startActivity(open);

执行此代码时,会显示新活动。但是当我点击智能手机上的最近按钮时,现在可以使用新活动和之前的活动了。
但在许多其他Android应用程序中,例如Whatsapp,如果你点击例如单击最近按钮时,您无法看到两项活动。 我也不认为他们都使用碎片,但我相信新的碎片也是活动 我该如何实现呢?

修改 这是一个完整的样本: 首先是我的activity_main.xlm:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.mb7.sportappbp.Activity.ActivityMain">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorWhite"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:titleTextColor="?attr/colorPrimary"
        app:title="FIT UP">
        <!--app:popupTheme="@style/AppTheme.PopupOverlay"-->


    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:background="@color/colorWhite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />

 </android.support.design.widget.CoordinatorLayout>

现在页面的代码:

public class ActivityMain extends AppCompatActivity {

...
private ViewPager mViewPager;
private TabLayout tabLayout;
private final String mainColor = "#2648FF";
public  static  User mainUser ;
public static ActivityMain activityMain;

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

        ...

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),this);

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    setTabLayout();
}
...

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


@Override
public boolean onContextItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings)
    {
        startActivity(new Intent(this,ActivitySettings.class));
        return true;
    }
    return super.onContextItemSelected(item);

}

@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) {
        startActivity(new Intent(this,ActivitySettings.class));
        return true;
    }
    return super.onOptionsItemSelected(item);
}

....

}

如您所见,我在onContextItemSelectedonOptionsItemSelected事件中调用了新活动。

新活动的布局:

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


    <ListPreference
        android:title="Abstand der Stimmungsabfrage"
        android:key="lstStmabfrageAbstand"
        android:defaultValue="3"
        android:summary="%s"
        android:entries="@array/ArrrayStmabfrageAbstand"
        android:entryValues="@array/ArrrayStmabfrageAbstandeValue" />
</PreferenceCategory>
<PreferenceCategory
    android:title="Erinnerung an das Training"
    android:key="pref_key_storage_settings">

    <ListPreference
        android:title="Fortbewegungsmittel zum Fitnessstudio"
        android:key="lstVerkehrsmittel"
        android:defaultValue="1"
        android:summary="%s"
        android:entries="@array/ArrrayVerkehrsmittel"
        android:entryValues="@array/ArrrayVerkehrsmittelValue" />
    <Preference
    android:title="Trainingszeiten und Studioadresse"
    android:summary="Wann und wo findet Ihr Training statt?">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetPackage="com.example.mb7.sportappbp"
        android:targetClass="com.example.mb7.sportappbp.Activity.ActivitySettingInitializer" />
</Preference>
    <Preference
        android:title="Kompass"
        android:summary="Zum Bearbeiten deiner Angaben im Kompass">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.mb7.sportappbp"
            android:targetClass="com.example.mb7.sportappbp.Activity.ActivityKompass" />
    </Preference>
</PreferenceCategory>

和代码:

public class ActivitySettings extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();
}
public static class SettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }

}

@Override
public void onBackPressed() {
//        Intent i = new Intent(this, ActivityMain.class);
//        i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//        startActivity(i);
    super.onBackPressed();
    finish();
    Toast.makeText(ActivityMain.activityMain,
            "Erfolgreich gespeichert" ,Toast.LENGTH_SHORT).show();
}


}   

现在当我在ActivityMain并转到ActivitySettings并点击最近按钮时,我会看到这两项活动,这就是我想要阻止的。< / p>

修改 这里是AndroidManifest.xml:

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

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.location.network" />

<application
    android:name=".Activity.FireApp"
    android:allowBackup="true"
    android:icon="@drawable/sport_icon"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".Activity.ActivityMain"
        android:label="@string/app_name"
        android:launchMode="singleInstance"
        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=".Activity.ActivityDiaryEntry"
        android:label="Tagebuch Eintrag"
        android:parentActivityName=".Activity.ActivityMain" />
    <activity
        android:name=".Activity.ActivityDiary"
        android:label="Tagebuch"
        android:parentActivityName=".Activity.ActivityMain" />
    <activity android:name=".Activity.ActivitySettings" />
    <activity android:name=".Activity.ActivityStimmungsAbgabe" />
    <activity android:name=".Activity.ActivitySettingInitializer" />
    <activity android:name=".Activity.ActivityChallenge" />
    <activity
        android:name=".Activity.ActivityCategories"
        android:label="Kategorie auswählen..."
        android:parentActivityName=".Activity.ActivityExerciseOverview" />
    <activity
        android:name=".Activity.ActivityExercises"
        android:parentActivityName=".Activity.ActivityCategories" />
    <activity android:name=".Activity.ActivityExerciseOverview" />
    <activity android:name=".Activity.ActivityFragebogen" />
    <activity android:name=".Activity.ActivityFitnessFragebogen" />
    <activity android:name=".Activity.ActivityMotivationMessage" />
    <activity android:name=".Activity.ActivityTrainQuestioning"></activity>
    <activity android:name=".Activity.ActivityKompass"></activity>
</application>

</manifest>

1 个答案:

答案 0 :(得分:1)

您是否尝试在AndroidManifest.xml中将“singleInstance”替换为“singleTask”? 您已经可以从文档中看到“singleInstance”没有为您提供所需内容。