0

Like this. It is working good from fragment to activity.

Intent intent = new Intent(Location.this,MessageActivity.class);
startActivity(intent);
peterh
  • 11,875
  • 18
  • 85
  • 108
Ahsan Malik
  • 399
  • 2
  • 3
  • 13

10 Answers10

10

You can't make intent for Fragment from Activity...there is fragment transaction to get Intent of Fragment in Activity.

Follow: FragmentTransaction

MyFragmentB fragmentB = new MyFragmentB();

To replace Fragment:

getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container, fragmentB)
                    .addToBackStack(MyFragmentA.class.getSimpleName())
                    .commit();

To Add Fragment:

getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, fragmentB)
                    .addToBackStack(MyFragmentA.class.getSimpleName())
                    .commit();

To pass Bundle to Fragment(From Activity to Fragment OR Fragment to Fragment) :

Bundle bundle = new Bundle();
bundle.putString("String", "String text");
bundle.putInt("Integer", Integer value);
bundle.putDouble("Double", Double value);
bundle.putBoolean("Boolean", Boolean value);
...
fragmentB.setArguments(bundle);

To get Bundle in Fragment:

getArguments().getString("String");//String text
getArguments().getInt("Integer");//Integer value
getArguments().getDouble("Double");//Double value
getArguments().getBoolean("Boolean");//Boolean value
Ready Android
  • 3,529
  • 2
  • 26
  • 40
3

Try this,

Fragment fragment = new TestFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
Komal12
  • 3,340
  • 4
  • 16
  • 25
3

To Intent from a fragment to another fragment follow below steps:

Step 0: When you have some fragments which are hosted by an activity, you should not intent directedly from fragment A to fragment B. So, you have to use your fragment container.

Step 1: In this step create an interface file in your project, and name it communicator

interface Communicator {
    fun sendData(userId:String)
}

this is our interface that we want to use it to send a data from a fragment to another fragment.

Step 2: go to your Main Activity and implement the interface:

class MainActivity : AppCompatActivity(), Communicator {



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // These three lines of code are belonged to Bottom Navigation
    val bottomNavigation = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
    val navHost = findNavController(R.id.fragmentContainerView)
    bottomNavigation.setupWithNavController(navHost)



}

override fun sendData(userId: String) {
    val bundle = Bundle()
    bundle.putString("userID", userId)
}

As you can see, we implemented the method that we already created in the communicator interface.

Step 3: Now, it is time to use supportFragmentManager to commute between fragments. (In Main Activity)

    override fun sendData(userId: String) {
    val bundle = Bundle()
    bundle.putString("userID", userId)

    var transaction = this.supportFragmentManager.beginTransaction()
    val fragmentB = FragmentB()
    fragmentB.arguments = bundle

    transaction.replace(R.id.fragmentContainerView, fragmentB )
    transaction.commit()

}

Above code tells us that fragmentB will be received a data from an interface. No we have to put the data into interface at fragmentA.

Step 4: Go to the fragment A, there you can put a data into the interface and get it into the fragment B.

class FragmentA : Fragment() {

    private lateinit var communicator: Communicator
    private lateinit var userIdForSend:String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false)


    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        communicator = activity as Communicator

        view.sendBtn.setOnClickListener{
           communicator.sendData(view.edtText.toString())
        }


    }

 
}

Step 5: Now we can get the data in the fragment B:

 class FragmentB : Fragment() {
    
        private var displayId:String? = ""
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
        }
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_home, container, false)
    
    
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    
            displayId = arguments?.getString("userID")
            view.txtView.text = displayId
     
    }

Done. Please take into your acount that this topic is really advance and you have to have some knowledge of android and Kotlin before you want to go for it.

hassan bazai
  • 424
  • 6
  • 9
2

just call.. finish()

same way, I can fix my problem.

1

You will need a framelayout that will hold the fragments and then add this with fragment_container being the id of the framelayout:

Fragment fragment = new MainFragment();
getSupportFragmentManager().beginTransaction()
                           .replace(R.id.fragment_container, fragment, ragment.getClass().getSimpleName())
                           .commit();
vanlooverenkoen
  • 2,121
  • 26
  • 50
1

You cannot pass the intent to the fragment, what you can do is get the data you have in the intent and pass it to the fragment. The recommended way to do that is to use the newInstance pattern.

Check this answer: How to pass intent from activity to fragment in Android

Community
  • 1
  • 1
nikeru8
  • 202
  • 2
  • 5
1

If you want to go fragment that is previously added to the stack. easy way to go that fragment is, clear the top of the activity stack.

For ex. MyFragment -> CurrentActivity, wants to go MyFragment from CurrentActivity..

Just clear the top of the activity stack.

Intent intent = new Intent(CurrentActivity.this,MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); finish();

here, MainActivity is the activity that hold the fragment.

0
Fragment myFragment  = new MyFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.youLayout, myFragment  
                            fragmentTransaction.addToBackStack("youfragment");
                            fragmentTransaction.commit();
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
arshad shaikh
  • 673
  • 8
  • 11
0

First of All Intent is used for Activity to activity as well as Fragment to Activity, You could not use Intent for Activity to fragment. Rather than Intent you can use "FragmentManager" here is the example:

public class MainActivity extends AppCompatActivity {

Button firstFragment, secondFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

firstFragment = (Button) findViewById(R.id.firstFragment);
secondFragment = (Button) findViewById(R.id.secondFragment);

firstFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

loadFragment(new FirstFragment());
}
});

secondFragment.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

loadFragment(new SecondFragment());
}
});

}

private void loadFragment(Fragment fragment) {
// create a FragmentManager
FragmentManager fm = getFragmentManager();
// create a FragmentTransaction to begin the transaction and replace the Fragment
FragmentTransaction fragmentTransaction = fm.beginTransaction();
// replace the FrameLayout with new Fragment
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commit(); // save the changes
}
}
Rajat Kalia
  • 1
  • 1
  • 2
0

Syntex :-

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace("Your Framelayout id", "Your fragment Name").addToBackStack(null).commit();

Example :-

 FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.main,new InquiryDashboardFragment()).addToBackStack(null).commit();