0

So I have Activity A with Fragment A.1, and I also have Activity B with Fragment B.1.

What I want to ask is, how do I move directly from Fragment A.1 to Fragment B.1?

I know to move from Fragment A.1 to Activity B, is by:

Intent i = new Intent (getActivity (), MainActivity.class);                     
startActivity (i);                     
getActivity ().finish();

But how to move straight to Fragment B.1? Each Activity A and Activity B has a different <FrameLayout> for Fragment replacement

UPDATE 1.0

I've tried my own way and also the way @cewaphi answered with code like this,

In Activity A:

Intent i = new Intent(TransactionDone.this, MainActivity.class);
i.putExtra("immediatelyTransactionToFragment", true);
startActivity(i);
finishAffinity();

In Activity B:

boolean shouldTransitionToFragment = getIntent().getBooleanExtra("immediatelyTransationToFragment", true);
if (shouldTransitionToFragment) {
Fragment fragment = new Wallet();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainFrameLayout, fragment);
transaction.commit();
Log.d("DEBUGGING REDIRECT", "Go to Fragment B.1");
}

The log "Go to Fragment B.1" was created but the transaction doesn't work

Muhammad Faisal
  • 734
  • 10
  • 24
  • So here is the first thing. Do you want activity B to launch with fragment B.1 while moving from your Activity A? If so I do not sure how to achieve, because to display your fragment B.1 UI, you must launch it inside any activity. So for your question, I need little clarification, 1-> Does your priority to just launch fragment B.1 or launch it with inside Activity B1? – K P Sep 05 '20 at 04:53
  • @KP I'm assuming I'm in Fragment A.1 in Activity A, and yes I want to be able to move to Fragment B.1 in Activity B. My reason isn't just launching Fragment B.1, because in Fragment B.1 there are a lot of operations So maybe I need to not just launch? – Muhammad Faisal Sep 05 '20 at 05:06
  • 1
    you can pass a variable with intent if intent has that extra in it just load the fragment in second activity – kelvin Sep 05 '20 at 05:08
  • 1
    if you want it, you must call your activity B which holds your Fragment B.1. There is no way you can directly call fragment as standalone according to my knowledge. You can do call Activity B and make sure to do your Fragment B.1 transaction in ActivtyB in oncCreate() using fragment transactions. In your use case easy solution to use fragment transaction with single activity and replace fragment inside ActivityA. – K P Sep 05 '20 at 05:10
  • @KP But... Fragment B.1 is now only accessible by click trigger in Activity B, it doesn't trigger automatically – Muhammad Faisal Sep 05 '20 at 05:14
  • @kelvin so I have to make logic in Activity B, if there is a value of 'example-value' then load Fragment B.1? – Muhammad Faisal Sep 05 '20 at 05:20
  • Yes . also why dont you replace Fragment A1 with fragment B1 inside Activity A . – kelvin Sep 05 '20 at 05:22
  • so I am assuming your Activity B does have some views and also fragment tag? Clicking on views you want to hide views and display fragments? If you want to Acess Fragment B 1 from Activity B, you must call Activity B from your Activity A. – K P Sep 05 '20 at 05:23
  • 1
    Is it really necessary to have multiple activities here? What you want to do becomes much easier when using the single activity approach in your app and especially when you are using the convenience of the [navigation component](https://developer.android.com/guide/navigation/navigation-migrate) – cewaphi Sep 05 '20 at 07:24
  • @cewaphi Yes I think so. but because this project is being worked on together, my other team worked on using 2 activities because they couldn't load the Webview responsively. – Muhammad Faisal Sep 06 '20 at 03:31
  • This problem should be easier to solve, when I only use one Activity which is used by all the Fragments. Thank you friend – Muhammad Faisal Sep 06 '20 at 04:28
  • If I may ask, why does another activity make the loading more responsive? – cewaphi Sep 06 '20 at 06:30
  • 1
    for your current approach: why do you not put an extra boolean in your intent when starting activity B from fragment A. 1? Then on receiving this boolean inside your activity B you move to fragment B. 1 after your activity has been created. If not for this boolean from Fragment A1 you only move from activity B with your button click. Is this the intended behavior? – cewaphi Sep 06 '20 at 06:36
  • @cewaphi Yeah right, that's the approach I want. Talking about other Activities makes loading more responsive, these are just words from my team. I just tried before and there was no problem – Muhammad Faisal Sep 06 '20 at 07:26

1 Answers1

0

When using a single activity and e.g. using the navigation component is not an option. Consider the following:

  • In your fragment A.1 when starting the activity store a Boolean

    i.putExtra("immediatelyTransationToFragment", true);
    
  • In activity B

    shouldTransitionToFragment = getIntent().getBooleanExtra("immediatelyTransationToFragment"); 
    // after activity was created
    if (shouldTransitionToFragment) {
        // Execute the transition action as you would when pressing the button
    }
    

Update 2020/09/07

You are trying to transition to a Fragment from your Activity like this:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainFrameLayout, fragment);
transaction.commit();

The documentation states that you should first add the fragment to the activity:

ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

Apparently your transition works when you click your button. Are you doing it the same? But I assume at that time the activity has already been created. Try once to add your fragment instead of replace. I don't know how your container is initialised but adding might be the operation you want, I refer to this good answer for clarification.

Also consider to perform to call this transaction after your activity was created.

cewaphi
  • 410
  • 2
  • 7
  • Can this directly trigger Begin Transaction to Fragment B.1? I have tried "If the value of the getStringExtra() result is not empty, then Begin Transaction to Fragment B.1." But it doesn't work. I've written it in onCreate () – Muhammad Faisal Sep 06 '20 at 17:21
  • Can you post the code how you try to transition to the fragment? If you do not have your `if` statement, does the transition (generally) not work? – cewaphi Sep 06 '20 at 18:34
  • To be honest, I am usually not conducting Fragment transactions manually anymore since I have switched to the convenience of the Navigation Component. I will update my answer accordingly for what I have noticed. – cewaphi Sep 07 '20 at 10:45
  • Yeah I thought so too, it's just a matter of time when I actually have to migrate using a navigation component. migrating all that current code would be very painful – Muhammad Faisal Sep 09 '20 at 06:38
  • By the way replacing `transaction.replace(R.id.mainFrameLayout, fragment);` with `fragmentTransaction.add(R.id.fragment_container, fragment);` doesn't solve the problem :( – Muhammad Faisal Sep 09 '20 at 08:33
  • I already assumed it wouldn't make a difference. How do you do it when you click the button? Does it work when you move after creating the activity (and not during creating it)? – cewaphi Sep 09 '20 at 14:18