私のAndroidアプリケーションで問題を解決しようとしています。Android - Intent&StartActivityでの重複ウィンドウの防止
問題は、IntentとStartActivityを呼び出して新しいインスタンスまたはクラスを開始すると、重複したウィンドウまたはビューが開くという問題です。
ビューに影響を与えずに、同じアクティビティまたはビューを保持したいが、新しいクラスを実行/実行したい。その目的は単に拡張クラスを実行することですが、再作成したり、重複したビューを開いたりしたくありません。
私はアンドロイドを使ってみました:Launchmode = "singleTop"しかし効果はありません。
私は標準的なアンドロイドのナビゲーションドロワーの例であるxmlとクラスを使用しています。 content_main.xmlには、新しいインスタンスやアクティビティをロードする必要のない2つのXMLファイルが含まれているviewswitcherが含まれていることがわかります。
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>
問題を解決しましたか?私はhttps://stackoverflow.com/questions/48024740/android-activity-multiple-instance – Pablo