1

There can be several instances of the same Activity in the task. My problem is that when I declare header-fragment like this:

    <fragment 
    android:name="fragments.TabletHeader"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:id="@+id/header" />

it stumbles on the second creation of the same activity. This is, I guess, because this static declaration forces system to create second insance of the same fragment which is not allowed. Am I right ?

What are the tactics for solving this issue. Essentially I need to switch to the dynamic approach but how do I find out whether fragment already exists. Show me some examples, please.

Here's the backtrace:

07-17 14:34:34.593: E/AndroidRuntime(15546): android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-17 14:34:34.593: E/AndroidRuntime(15546):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at android.app.Activity.setContentView(Activity.java:1835)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at android.activity.MainActivity$1.dispatchMessage(MainActivity.java:112)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at android.os.Looper.loop(Looper.java:137)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at android.app.ActivityThread.main(ActivityThread.java:4514)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at java.lang.reflect.Method.invoke(Method.java:511)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at dalvik.system.NativeStart.main(Native Method)
07-17 14:34:34.593: E/AndroidRuntime(15546): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f08000e, tag null, or parent id 0x0 with another fragment for android.fragments.TabletHeader
07-17 14:34:34.593: E/AndroidRuntime(15546):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:275)
07-17 14:34:34.593: E/AndroidRuntime(15546):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
07-17 14:34:34.593: E/AndroidRuntime(15546):    ... 14 more
user1462299
  • 4,247
  • 5
  • 22
  • 30

1 Answers1

0

I have not been using fragments from XML, but have used the viewpager enough to see a lifetime of issues. One suggestion I can make for you to look into is the Tag that is set. I know the format of the tag when a FrameAdapter is used. When adding using a transaction, you can define your own tag. In the case of xml, I suspect that the Tag may be the Name parameter.

So, you can use the findFragmentByTag(String tag) function and try to use the value of the Name field. If this gives you a valid fragment, you are all set.

Hope this helps. Again, the answer is an assumption which you can try.

EDIT:Another option may be to look into findFragmentById(R.id.header)

Aviral
  • 1,141
  • 1
  • 10
  • 16
  • How does it help solving the issue? My app gets crushed because of the ID declaration. I don't know how to overcome it – user1462299 Jul 17 '12 at 11:47
  • Thought you were looking for a way to determine if the fragment existed on start of the activity. If I misunderstood, please ignore. – Aviral Jul 17 '12 at 13:02