我正在尝试使用导航图在两个片段之间导航。由于某种原因,当我使用图形的生成操作调用导航方法时,目标片段正在“创建”但没有出现,而起始片段就像已被停用一样,但不会消失。我想我已经遵循了Google 文档页面上列出的所有步骤,但我可能遗漏了一些简单的东西!
这是我的依赖项:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// ViewModel and LiveData
def lifecycle_version = "2.1.0"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
def nav_version = "2.1.0"
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
主要活动
在我的主要活动类中,我只是定义了ModelViewin OnCreate():
public class MainActivity extends AppCompatActivity {
MealDataModel mealDataModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the model on create of the main activity
mealDataModel = ViewModelProviders.of(this).get(MealDataModel.class);
}
}
在我的主要活动布局中,我只使用 NavHostFragment:
...
<fragment
android:id="@+id/fragment2"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
...
陪伴而非守候
慕桂英3389331
相关分类