2

I have a class which extends to fragment and there is a button on it once click it initialises a new class which extends to an activity.. i wanted to pass data(e.g. string) on button click from that activity back to the caller fragment.

FirstActivity extends fragment

on button click

Intent startcontact = new Intent(getActivity(), Contact.class);
                getActivity().startActivityForResult(startcontact,1);

Contact extends activity

on button click

Bundle bundle = new Bundle();
                 bundle.putString("ListofContacts", tapo1);
                 Intent intent = new Intent(Contact.this, First_Activity.class);
                 intent.putExtras(bundle);
                 setResult(RESULT_OK);
                 startActivity(intent);
                 finish();

FirstActivity onactivityresult

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
         MultiAutoCompleteTextView txtPhoneNo = (MultiAutoCompleteTextView) getActivity().findViewById(R.id.txtPhoneNo);
         String strtxt = null;
         if (requestCode == 1) 
         {
            if(resultCode == getActivity().RESULT_OK)
            {
             strtxt=data.getStringExtra("ListOfContacts");
             txtPhoneNo.setText(strtxt);
            }
         }
    }

when i tried to click the button from the contact class i got an error:

ClassCastException: FirstActivity cannot be cast to android.app.activity

pls help!

logcat:

08-15 23:26:39.153: E/AndroidRuntime(21267): FATAL EXCEPTION: main
08-15 23:26:39.153: E/AndroidRuntime(21267): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.john/com.example.john.First_Activity}: java.lang.ClassCastException: com.example.john.First_Activity cannot be cast to android.app.Activity
08-15 23:26:39.153: E/AndroidRuntime(21267):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2001)
08-15 23:26:39.153: E/AndroidRuntime(21267):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
08-15 23:26:39.153: E/AndroidRuntime(21267):    at android.app.ActivityThread.access$600(ActivityThread.java:134)
08-15 23:26:39.153: E/AndroidRuntime(21267):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
08-15 23:26:39.153: E/AndroidRuntime(21267):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 23:26:39.153: E/AndroidRuntime(21267):    at android.os.Looper.loop(Looper.java:154)
08-15 23:26:39.153: E/AndroidRuntime(21267):    at android.app.ActivityThread.main(ActivityThread.java:4624)
08-15 23:26:39.153: E/AndroidRuntime(21267):    at java.lang.reflect.Method.invokeNative(Native Method)
08-15 23:26:39.153: E/AndroidRuntime(21267):    at java.lang.reflect.Method.invoke(Method.java:511)
08-15 23:26:39.153: E/AndroidRuntime(21267):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
08-15 23:26:39.153: E/AndroidRuntime(21267):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
08-15 23:26:39.153: E/AndroidRuntime(21267):    at dalvik.system.NativeStart.main(Native Method)
08-15 23:26:39.153: E/AndroidRuntime(21267): Caused by: java.lang.ClassCastException: com.example.john.First_Activity cannot be cast to android.app.Activity
08-15 23:26:39.153: E/AndroidRuntime(21267):    at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
08-15 23:26:39.153: E/AndroidRuntime(21267):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1992)
08-15 23:26:39.153: E/AndroidRuntime(21267):    ... 11 more
RieJack
  • 51
  • 2
  • 10

4 Answers4

1

The intent needs an activity. If you don't want to change FirstActivity to an Activity, and would rather keep it as a fragment, you could do a fragment transaction instead of an intent in order to push the fragment. Below would be the following code to do so:

final FragmentManager manager = getSupportFragmentManager();
FirstActivity fragment = new FirstAcitivty();
final FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.content_frame, mArticleFragment, null);
transaction.commit();

The activity that you want to put this fragment into also needs a FrameLayout, a place to inflate the fragment into. You can add a FrameLayout to your xml like so:

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

If you put in your xml for the activity a hard coded fragment, then you can't do the transaction, so make sure that you switch to the FrameLayout for the place where you inflate the Fragment.

Jordan B.
  • 143
  • 7
  • Thanks sir Jordan.. I was confused I'm sorry I just wanted to pass a string from an activity back to the caller fragment.. pls bear with me .. sorry – RieJack Aug 15 '13 at 15:12
  • You can still pass information from the fragment to the activity and vice versa using the bundle that was mentioned in another post – Jordan B. Aug 19 '13 at 14:56
1

you should't start your First_Activity again. you should just call finish() to get back to the First_Activity and pass the string you want to that class like this:

Intent intent = new Intent();
intent.putExtra("ListofContacts", tapo1);
setResult(1, intent);
finish();

and in onActivityResult you will be able to get the string extra you needed from data.

arianoo
  • 657
  • 14
  • 22
  • hi arianoo, seems like you dont start the activity. – RieJack Aug 15 '13 at 15:18
  • the `First_Activity` doesn't need to be started again as it started your `Contact` activity and when the `finish()` of the Contact activity gets called, you will get back to the First_Activity automatically and it's `onActivityResult` gets executed automatically – arianoo Aug 15 '13 at 15:21
  • i just tried your solution and it worked but i wanted to set the text to a text view doing so doesnt set the txt.. you have any idea? – RieJack Aug 15 '13 at 15:38
  • @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); MultiAutoCompleteTextView txtPhoneNo = (MultiAutoCompleteTextView) getActivity().findViewById(R.id.txtPhoneNo); String strtxt = null; if (requestCode == 1) { if(resultCode == getActivity().RESULT_OK) { strtxt=data.getStringExtra("ListOfContacts"); txtPhoneNo.setText(strtxt); } } } – RieJack Aug 15 '13 at 15:39
  • does it return any errors when ` txtPhoneNo.setText(strtxt);` gets executed? what do you see happening when you debug the code? – arianoo Aug 15 '13 at 15:48
  • i got no error from the logcat.. i wanted to put the string to a textview when button is click – RieJack Aug 15 '13 at 15:52
  • I guess it's a new question and should be asked separately with all the details. if this answer solved this question that you asked and the strtxt is passed correctly when returning to the first_activity, then you can mark my answer as an answer and ask you next question apart from it – arianoo Aug 15 '13 at 15:56
  • post it as another question SO is also complaining about our discussion in comments in here and i can't invite u to chat unfortunately. – arianoo Aug 15 '13 at 16:30
-2

you are using onActivityResult in your Fragment FirstActivity.class but only an Activity class can use this method and receive the result code, not a Fragment. You can use flags to check the change in value and then check in the onResume() in the Fragment FirstActivity.class

arshu
  • 11,805
  • 3
  • 23
  • 21
  • cannot do.. my FirstActivity is a part of the whole thing. its like i created tabs using fragments. do you have any suggestion how could i implement this scenario? – RieJack Aug 15 '13 at 14:41
  • You can this answer of mine on SO, i hope it might help you. Still if you are confused then just invite me on chat I would try to do my best for you dude :) http://stackoverflow.com/questions/18232278/how-to-create-tab-in-android-using-tabactivity-or-fragmentactivity/18232540#18232540 – arshu Aug 15 '13 at 14:47
  • im sorry First_Activity and FirstActivity is just the same.. just clerical error. – RieJack Aug 15 '13 at 14:49
  • Thanks Arshad. Im gonna try my best – RieJack Aug 15 '13 at 14:50
  • Sorry I cant invite you to chat i still need to earn 20 reputations. – RieJack Aug 15 '13 at 15:20
  • in stuck in an Android Help Desk something.. I cant make a chat to you – RieJack Aug 15 '13 at 15:46
  • Ok , just paste your FirstActivity code. Let me see what you are doing in it . – arshu Aug 15 '13 at 15:52
-2

ok ok im changing my answer then:

according to the error you didn't extend the class with "Activity". the type of your FirstActivity is Fragment. so you have two options now:

1: FirstActivity extends Activity

2: FirstFragment extends Fragment


The basic concept is to have an activity that hosts a fragment-container for you. Then you have your seperate Fragment-class that contains all the necessary stuff to build the fragment('s view). The FragmentManager of the activity handles the loading of the fragments content.
Does that help you?

bofredo
  • 2,348
  • 6
  • 32
  • 51
  • Read the question. It says "FirstActivity extends fragment". Also off topic to write an comment as an answer. – David Olsson Aug 15 '13 at 14:35
  • FirstActivity is just a class name which extends to a fragment. do you have any idea how to handle this.. im still a beginner. im sorry – RieJack Aug 15 '13 at 14:44
  • you should try to read some basic text explaining the idea of activities & fragments first as it looks like that you just try to use stuff you see here without a concept. Can't say more now, as mean-guy will downvote me more :) – bofredo Aug 15 '13 at 14:52
  • i changed my answer, hope it gives you some indication of the problem. – bofredo Aug 15 '13 at 15:00