0

If I click Activity-3 Back Button then Its going to Activity-1 instead of Activity-2. I am not sure what's wrong in the Manifest file.Error If I click the Back Button. Below is the Error code from the Activity Page 2. Recycler view is working fine if I click the item. Only backward is the issue now.
Found the Issue. The Variable from Activity3 to Activity2 is null so thats why Its going to 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>
  • Perhaps the problem is in your code. How do you start your Activities ? – Bruno Oct 18 '18 at 09:21
  • customView.setOnClickListener { val intent= Intent(customView.context,Activity3::class.java) intent.putExtra(Activity_KEY,List?.name) customView.context.startActivity(intent) println("Test Menu Clicks") –  Oct 18 '18 at 17:45
  • Did you use retrofit for json parsing – Shaon Oct 18 '18 at 17:47
  • implementation 'com.squareup.okhttp3:okhttp:3.11.0' implementation 'com.google.code.gson:gson:2.8.5' –  Oct 18 '18 at 17:50

2 Answers2

0

Use android:parentActivityName:

<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>
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • Thanks @Mohsen for your response but its not working. –  Oct 18 '18 at 08:42
  • Try using it without `meta-data`. it should work with only `android:parentActivityName` attribute. – ʍѳђઽ૯ท Oct 18 '18 at 08:42
  • I found the Problem. I am using JSON Array in my Activity 2. If I Click from Activity3 to Activity 2 the Variable required for JSON String is empty thats why I am getting this error. Is there any way to store the variable in session so, once I click Activity 2 from 3 then I can retrieve the values. –  Oct 18 '18 at 21:03
  • That's another question which should be pasted with related codes. However, are you asking to save data? If so, check this and see if it helps: https://stackoverflow.com/a/26131561/4409113 – ʍѳђઽ૯ท Oct 19 '18 at 14:24
0

You don't need to have a parent activity to Navigate Up / Go back. But if you want so, you need to specify android:parentActivityName attribute, otherwise it won't work with Android versions above 4.0. From 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>

Other way to navigate up is using the back button implementation. Considering you will stack the activities on the top of the other,

Inside OnCreate method add this:

if (getSupportActionBar() != null) 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Then override OnOptionsItemSelected implementing an action:

@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);
}
Dimas Mendes
  • 2,664
  • 1
  • 21
  • 19