Reputation: 439
There are so many ways but i am using this one:
Activity:(Main2Activity)
public class Main2Activity extends AppCompatActivity{
private String myString = "hello";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
...
}
public String getMyData() {
return myString;
}
}
Fragment:
public class MyFragment extends android.support.v4.app.Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Main2Activity activity = (Main2Activity ) getActivity();
String myDataFromActivity = activity.getMyData();
return view;
}
}
Main Activity:
public class MainActivity extends AppCompatActivity {
-------
@Override
protected void onCreate(Bundle savedInstanceState) {
}
---
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new MyFragment(), "MyFragment");
viewPager.setAdapter(adapter);
}
BUT its giving me class cast exception:
java.lang.ClassCastException: com.example.dev03.xyz.Activities.MainActivity cannot be cast to com.example.dev03.xyz.Fragments.MyActivity
java.lang.ClassCastException: com.example.dev03.xyz.Fragments.MainActivity cannot be cast to com.example.dev03.xyz.Fragments.Main2Activity
-- Package is same.
Thanks
Upvotes: 2
Views: 825
Reputation: 439
I searched for a whole day , Finally i got the perfect solution: If anyone having the same problem you can follow this answer.
NO need to create any method in Main2Activity and call it from MyFragment. Best practice is , use startActivityForResult and onActivityResult. Here is the code:
Fragment:
public class MyFragment extends android.support.v4.app.Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
Intent i = new Intent(getActivity(), Main2Activity.class);
startActivityForResult(i, 1);
}
}
Main2Activity:
public class Main2Activity extends AppCompatActivity{
private String myString = "hello";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
...
Intent returnIntent = new Intent();
returnIntent.putExtra("result", selectedPath1);
returnIntent.putExtra("result2", selectedPath2);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
}
Simple.
Upvotes: 1
Reputation: 61
Create an object of MainActivity2 and simply call your getMyData method from Fragment Class
Upvotes: 1
Reputation: 4959
If you want to done this work you must have to keep your myFragment into Main2Activity instead of MainActivity.
Upvotes: 1
Reputation: 9225
Create an object of MainActivity2
and access the method as follow:
MainActivity2 mn2=new ManiActivity2();
mn2.getMyData();
Upvotes: 1
Reputation: 5655
In your Activity, do the following
MyFragment fragmentObj=new MyFragment();
Bundle args = new Bundle();
args.putString("Data_Key", string_value);
fragmentObj.setArguments(args);
Now add this Fragment to stack using FragmentTransaction
.
After this, in your Fragment class's onCreateView
do the following
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_xml_view, container, false);
if (getArguments() != null)
{
String valueReceived = getArguments().getString("Data_Key");
}
return rootView;
}
Upvotes: 1
Reputation: 14825
Try making your getMyData()
static
public static String getMyData() {
return myString;
}
and then access it in fragment like
Main2Activity.getMyData();
Upvotes: 1
Reputation: 184
You create your Fragment in MainActivity and it belongs to it, so you get MainActivity instead of Main2Activity when you use getAcitvity().
Upvotes: 3