2

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

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
Minkoo
  • 439
  • 1
  • 3
  • 16

7 Answers7

3

You create your Fragment in MainActivity and it belongs to it, so you get MainActivity instead of Main2Activity when you use getAcitvity().

TmCrafz
  • 184
  • 1
  • 12
  • Yeaaah !!! , this seems to be meaningful .(+1)..And what should i use here in the place of getActivity. – Minkoo May 06 '16 at 12:04
1

Try making your getMyData() static

public static String getMyData() {
return myString;

}

and then access it in fragment like

Main2Activity.getMyData();
Atiq
  • 14,435
  • 6
  • 54
  • 69
1

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;
}
Sreehari
  • 5,621
  • 2
  • 25
  • 59
  • It's gud .. but i am following a different way, Not using Transaction or Bundle. I appreciate. Thanks buddy – Minkoo May 06 '16 at 12:24
  • @Minkoo This is the standard way. Using getter setter is not advisable. – Sreehari May 06 '16 at 12:26
  • Thanks buddy, n you are right , today i read about and even in your it has loose coupling. Thanks i will definitely use this method later. – Minkoo May 06 '16 at 12:53
1

Create an object of MainActivity2 and access the method as follow:

MainActivity2 mn2=new ManiActivity2();
mn2.getMyData();
Android Geek
  • 8,956
  • 2
  • 21
  • 35
  • You can't create object of Activity. – Piyush May 06 '16 at 12:13
  • Yesssssss !!! It was a silly mistake but you got that silly one. Thanks Buddy ... – Minkoo May 06 '16 at 12:26
  • @Minkoo May be it is working. But creation object of activity is a bad practice for programmers ! – Piyush May 06 '16 at 12:29
  • @Piyush ...Nouuu ,I used this, i made object of MainActvity2 and by using this object i got the result of MainActivity2 in My Fragment class. – Minkoo May 06 '16 at 12:31
  • Yeah !! I agree ..even i should use Bundle or Transaction to send data ... My one has method has tight coupling... But You are also right. Thanks Buddy – Minkoo May 06 '16 at 12:33
  • @Minkoo Read answer here of Raghav Sood http://stackoverflow.com/questions/14956018/can-i-create-the-object-of-a-activity-in-other-class – Piyush May 06 '16 at 12:36
1

If you want to done this work you must have to keep your myFragment into Main2Activity instead of MainActivity.

Masum
  • 4,879
  • 2
  • 23
  • 28
  • I appreciate , but it was a silly mistake ... i should use like , MainActivity2 mn2=new ManiActivity2(); mn2.getMyData(); – Minkoo May 06 '16 at 12:28
1

Create an object of MainActivity2 and simply call your getMyData method from Fragment Class

G Setia
  • 61
  • 6
1

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.

Minkoo
  • 439
  • 1
  • 3
  • 16