user587550
user587550

Reputation: 21

onActivityResult not getting result

I simply want to return the string with intent from a another activity but not getting that string in parent activity...though no error is shown. for test purpose just want to show toast message on return of result..

here's the code of parent activity....

public class Login2 extends Activity {
     static final int REQ_CODE=0;
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

      Button startButton=(Button) findViewById(R.id.btn_login);
        startButton.setOnClickListener(new View.OnClickListener()
        {
         public void onClick(View view)
         {          
          final TextView var=(TextView)findViewById(R.id.errmsg);

          EditText usrnameobj = (EditText)findViewById(R.id.et_usename);
          EditText usrpassobj = (EditText)findViewById(R.id.et_password); 
          String username = usrnameobj.getText().toString(); 
          String password = usrpassobj.getText().toString(); 

          Intent i=new Intent(getApplicationContext(),AndroidXmlResource.class);
          i.putExtra("entry",username.toString() + password.toString());
          startActivityForResult(i,REQ_CODE);
         }  
         protected void onActivityResult(int requestCode, int resultCode, Intent data)

            {

          //if (resultCode == Activity.RESULT_OK && requestCode == 0) 
           {

                      //String res = data.getExtras().getString("result");
                     // TextView var=(TextView)findViewById(R.id.errmsg);
                     // var.setVisibility(0);
                     // var.setText(res.toString());
                     // var.setText("called on act res");

                      Toast.makeText(Login2.this,"Hi! Bright Hub", Toast.LENGTH_SHORT).show();
                  }
            }
        });   

And here's the child activity that returns a result....

public class AndroidXmlResource extends Activity {

 //public String U="sudeep1";
 //public String P="donotlock1";
 public int Res;
 @Override
 protected void onCreate(Bundle bundle) {
  // TODO Auto-generated method stub
  super.onCreate(bundle);
  //setContentView(R.layout.main);
  Bundle extras = getIntent().getExtras();
  if (extras == null) {
   return;
  }
  String pass=extras.getString("entry");
  //String result= new String();
        //TextView myXmlContent = (TextView)findViewById(R.id.tv_test);

  try {
   String stringXmlContent = getEventsFromAnXML(this);
   //myXmlContent.setText(pass.toString());
   Intent returnIntent = new Intent();
   if(stringXmlContent.equals(pass))
   {

    returnIntent.putExtra("result","valid Login");
    setResult(RESULT_OK,returnIntent);   
       super.finish();

   }
   else
   {
    returnIntent.putExtra("result","Invalid Login");
       setResult(RESULT_OK,returnIntent);   
       super.finish();

   }


  } catch (XmlPullParserException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    }

    private String getEventsFromAnXML(Activity activity)
    throws XmlPullParserException, IOException
    {

     StringBuffer stringBuffer = new StringBuffer();
     Resources res = activity.getResources();
     XmlResourceParser xpp = res.getXml(R.xml.myxml);
     xpp.next();
     int eventType = xpp.getEventType();
     while (eventType != XmlPullParser.END_DOCUMENT)
     {
      int c=1;
      if(eventType == XmlPullParser.START_DOCUMENT)
      {
       //stringBuffer.append("--- Start XML ---");
      }
      else if(eventType == XmlPullParser.START_TAG)
      {
       //stringBuffer.append("\nSTART_TAG: "+xpp.getName());

      }
      else if(eventType == XmlPullParser.END_TAG)
      {
       //stringBuffer.append("\nEND_TAG: "+xpp.getName());
      }
      else if(eventType == XmlPullParser.TEXT)
      {
       //stringBuffer.append("\nTEXT: "+xpp.getText());
       //extra

       { String t=xpp.getText().toString();

         stringBuffer.append(t.toString());      

       }       
      }

      eventType = xpp.next();
     }
     //stringBuffer.append("\n--- End XML ---");
     return stringBuffer.toString();

Upvotes: 1

Views: 5443

Answers (5)

Julian Mancera
Julian Mancera

Reputation: 304

I had a similar problem and the solution that I found was to replace the startActivityForResult functionality by storing the result into the shared preferences and loading them later in onResume. Check the full discussion here.

Upvotes: 0

Atul Bhardwaj
Atul Bhardwaj

Reputation: 6717

Make sure that you have not added this line android:launchMode="singleInstance|singleTask" in menifest.xml where you have declared your activty .If you have added, remove this for getting result on onActivityResult().

Upvotes: 1

Eugene Petrenko
Eugene Petrenko

Reputation: 4992

Make sure you do not have android:noHistory="true" attribute for element.

Upvotes: 2

Pending Intent
Pending Intent

Reputation: 121

Can you please put @Override onActivityResult() and another one in setResult(Activity.RESULT_OK,intent)?

Make sure this in child activity.

Upvotes: 1

Aman Aalam
Aman Aalam

Reputation: 11251

in your parent activity, you should be doing this:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{

if (resultCode == Activity.RESULT_OK && requestCode == REQ_CODE) 
   {

    String res = data.getExtras().getString("result");
    Toast.makeText(Login2.this,"Hi! Bright Hub: "+res, Toast.LENGTH_SHORT).show();
   }
 }

Which you're already using.

what does super.finish() do? why not this.finish() ??

Upvotes: 1

Related Questions