Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

android - how to switch between fragments of two different activity class

I have two different activities, A & B. Both have NavigationDrawer , look alike, but not are the same, because I could not get the drawer layout ID of activity A in activity B.

Both activity has 3 fragments each (total 6).

The NavigationDrawer contains all fragments of activity class A only. My problem is, when I am in activity B, and try to open one fragment of activity A from navigation drawer, it throws an error

No view found for id 0x7f090047 (com.wlodsgn.bunbunup:id/linear) for fragment FmMenu{b1e537f0 #0 id=0x7f090047}

How do I achieve it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have created an intent inside the second activity and started the activity A with information about the fragment to be called.

Intent i = new Intent(this, ActivityClass.class);
i.putExtra("frgToLoad", "FRAGMENT_A");
startActivity(i);

Now, inside activity A, checked the extra and load the right Fragment:

OnCreate(){
...
if (getIntent().getExtras() != null) {
String intentFragment = getIntent().getExtras().getString("frgToLoad");

switch (intentFragment){
    case "FRAGMENT_A":
        // Load corresponding fragment
        break;
    case "FRAGMENT_B":
        // Load corresponding fragment
        break;
    case "FRAGMENT_C":
        // Load corresponding fragment
        break;
   }
}
}

One have to check if intent is null or not before I try to assign a value to intentFragment. This is because that line of code is called whether I am coming from activity B or not, and it will throw error if intent is null.

credit : https://stackoverflow.com/a/36064344/3380537


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...