如果我单击Activity-3后退按钮,则它将转到Activity-1,而不是Activity-2。我不确定清单文件中有什么问题。如果单击“后退”按钮,则会出现错误。
下面是“活动”页面2中的错误代码。如果单击该项目,则Recycler视图工作正常。现在的问题只是向后。
找到了问题。从Activity3到Activity2的变量为空,因此这就是它进入Activity1的原因。
Activity:3
TestMenuDetail:
val navBarTitle2=intent.getStringExtra(TestMenuViewHolder.TEST_TITLE_NAME)
val TestVar=Intent(this@TestMenuDetail,TestMenuList::class.java)
intent.putExtra("TestVar",navBarTitle2)
Activity:2
TestMenuList:
val navBarTitle3=intent.getStringExtra("TestVar")
println("Helllo Test: $navBarTitle3")
Process: TestProject, PID: 28725
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was NUMBER at line 1 column 2 path $
at com.google.gson.Gson.fromJson(Gson.java:939)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)
at TestProject.TestMenuList$fetchJSON$1.onResponse(TestMenuList.kt:50)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was NUMBER at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)
at TestProject.TestMenuList$fetchJSON$1.onResponse(TestMenuList.kt:50)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Android Manifest.XML
Activity:1
<activity
android:name="TestProject.ItemDetailActivity3"
android:label="Item Price"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="TestProject.WelcomeActivity" />
</activity>
Activity:2
<activity
android:name="TestProject.TestMenuList"
android:label="Test Menu"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="TestProject.ItemDetailActivity3" />
</activity>
Activity:3
<activity android:name="TestProject.TestMenuDetail"
android:label="Test List Detail"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="TestProject.TestMenuList" />
</activity>
答案 0 :(得分:0)
<activity android:name="TestProject.TestMenuDetail"
android:label="Test List Detail"
android:screenOrientation="portrait"
android:parentActivityName="TestProject.TestMenuList"> // your activity which you want to get back to
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="TestProject.TestMenuList" />
</activity>
答案 1 :(得分:0)
您不需要上级活动即可导航/返回。但是,如果需要,则需要指定android:parentActivityName
属性,否则它不适用于4.0以上的Android版本。来自Google Docs:
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
其他向上导航的方法是使用后退按钮实现。考虑到您会将活动堆叠在另一个活动的顶部,
在OnCreate
方法内部添加以下内容:
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
然后覆盖OnOptionsItemSelected
来执行操作:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}