【发布时间】:2017-01-31 23:11:36
【问题描述】:
我正在尝试解决我的 android 应用程序的问题。
问题是,当我通过调用 Intent 和 StartActivity 启动新实例或类时,会打开一个重复的窗口或视图。
我想保留相同的活动或视图,但在不影响视图的情况下执行/运行新类。目的只是执行扩展类,但我不希望它重新创建或打开重复视图。
我尝试过使用 android:Launchmode="singleTop" 但没有效果。
我使用了标准的 android 抽屉式导航示例 xml 和类。您将看到 content_main.xml 包含一个视图切换器,其中包括 2 个其他 xml 文件,它们不需要加载新实例或活动......如果这有意义的话。
我不确定问题是否出在 BeaconTracking.java 上,它再次调用 super.onCreate(...) 事件可能导致父视图重新打开??
任何想法我哪里出错了?
提前致谢!
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".BeaconTracking"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
</activity>
MainActivity.java
public class MainActivity extends AppCompatActivity implements ...
public ViewSwitcher switcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_vehicle_tracking) {
Intent intent = new Intent(getApplicationContext(), BeaconTracking.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
} else if (id == R.id.nav_vehicle_info) {
//SWITCH TO BEACON SCREEN
switcher.setDisplayedChild(2);
}
//CLOSE NAVIGATION DRAWER WHEN BUTTON IS PRESSED
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
BeaconTracking.java
public class BeaconTracking extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
switcher.setDisplayedChild(1);
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/content_frame"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/content_beacons" />
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/content_map" />
</ViewSwitcher>
【问题讨论】:
-
您的问题解决了吗?我正在为stackoverflow.com/questions/48024740/…之类的东西苦苦挣扎
标签: android android-intent android-activity launchmode