-3

In Fragment, click on listView item to open new activity and pass value to other activity

   list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    sowing sowing=new sowing();
                    FragmentManager fragmentManager=getFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.FragmentContainer,sowing,sowing.getTag())
                            .addToBackStack("fragBack").commit();
                    String selectedFromList =(list.getItemAtPosition(position).toString());                
        }
    });
Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
  • 1
    Although I get what you're trying to do from the title, can you please provide a description in your body and re-format the code so we can see what you've tried. A simple `CTRL + K` and pasting in the new indented line should fix the formatting. [StackOverflow How to Ask](https://stackoverflow.com/help/how-to-ask) – Mwikala Kangwa May 21 '18 at 11:05
  • Duplicate of https://stackoverflow.com/questions/5885489/passing-values-between-activities-in-android – Luís Henriques May 21 '18 at 11:29

2 Answers2

0

The best way is to use SharedPreferences.

https://developer.android.com/reference/android/content/SharedPreferences

https://developer.android.com/training/data-storage/shared-preferences

Luís Henriques
  • 604
  • 1
  • 10
  • 30
  • i simply want to pass value to other activity and catch in other activity – Rahul Dhande Patil May 21 '18 at 11:18
  • I know that. Please refer to: https://stackoverflow.com/questions/5885489/passing-values-between-activities-in-android Also, please do your research next time before posting questions. We want to avoid duplicates, as stated in the posting guidelines. – Luís Henriques May 21 '18 at 11:28
  • 1
    I think using SharedPrefrences in this case is **very** bad practise because SharedPreferences will stay until cleared whereas [Bundles](https://developer.android.com/reference/android/os/Bundle) are passed from Intent to Activity. – Mwikala Kangwa May 21 '18 at 11:37
0

What you are looking for is Bundles

Here is an example of how you'd use it in your case:

list.setOnClickListener(new AdapterView.onItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    sowing sowing = new sowing();
    FragmentManger fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.FragmentContainer, sowing, sowing.getTag())
        .addToBackStack("fragBack").commit();
    String selectedFromList = (list.getItemAtPosition(position).toString));

    // THE BUNDLE
    // start a new intent to open the activity
    Intent newIntent = new Intent(Context, Activity.class);
    newIntent.putStringExtra("NAME_OF_BUNDLE", selectedFromList); // I used putStringExtra because you defined 'selectedFromList' to be a string
    startActivity(newIntent);
   }
});

Now in the activity you opened just call the name of the bundle like so:

Intent intent = getIntent(); // get the intent that caused this activity to be opened
String selectedFromLastActivity = intent.getStringExtra("NAME_OF_BUNDLE"); // from the intent that caused this activity to be opened, get any extras passed through
Mwikala Kangwa
  • 430
  • 7
  • 24