开发者

One activity call dialog & return to previous activity

开发者 https://www.devze.com 2023-03-27 05:56 出处:网络
My requirement is One activity is calling another activity.When I click the radio button from list of sales route in the parent activity , dialog(that is called child) will come.That contain form. Aft

My requirement is One activity is calling another activity.When I click the radio button from list of sales route in the parent activity , dialog(that is called child) will come.That contain form. After finish tiny form work , it go to previous place(need to continue rest of work).

I have done like this:

Androidmanifest.xml

 <activity android:theme="@android:style/Theme.Light.Panel" android:name=".SalesRouteDevitionActivity"
              android:label="Sales Route Diviation">
    </activity>

Then My list of sales route java part is:

 @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Object o = this.getListAdapter().getItem(position);
    keyword = o.toString();
    positions = position;

    if(position != 0 ){
        Bundle bundle = new Bundle();

        Intent showContent = new Intent(v.getContext(),SalesRouteDevitionActivity.class);
        int postion = position;
        String aString = Integer.toString(postion);
        bundle.putString("positon", aString);
        showContent.putExtras(bundle);
        startActivityForResult(showContent, 2);


    }else{

        Intent intent = new Intent(SalesRouteActivity.this, ListRetailerActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("RouteName", keyword);
        intent.putExtras(bundle);
        View view = SalesActivityGroup.group.getLocalActivityManager().startActivity("", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();  
        SalesActivityGroup.group.replaceView(view);
    }


}

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if( resultCode==Activity.RESULT_OK){
          Toast.makeText(this, "Reason has been successfully.", Toast.LENGTH_LONG).show();
          if(resultCode==RESULT_OK)
              Toast.makeText(this, "Reason has been successfully.", Toast.LENGTH_LONG).show();
         else
              Toast.makeText(this, "Fail", Toast.LENGTH_LONG).show();
      }

 // super.onActivityResult(requestCode, resultCode, data);
  }

And my child activity is :

      public class SalesRouteDevitionActivity extends Activity {
private String array_spinner[];
String param1 = "";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.route_diviation_popup);
    array_spinner=new String[2];
    array_spinner[0]="Rain";
    array_spinner[1]="Floods";

    Spinner s = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, array_spinner);
    s.setAdapter(adapter);
    Button button = (Button) findViewById(R.id.submit);

     button.setOnClickListener(new OnClickListener() {
         public开发者_开发百科 void onClick(View v) {
             Intent intent= new Intent(getApplicationContext(),SalesRouteActivity.class);
             Log.i("*********" ," --- " +getParent());
             if (getParent() == null) {
                 setResult(RESULT_OK, intent);
             } else {
                 getParent().setResult(RESULT_OK, intent);
            }
             finish();
         }
     });
}

My problem is after finish popup activity, It didn't go to onActivityResult() method

I have a doubt, if the theme is @android:style/Theme.Dialog then can we use onActivityResult();

Please help me ...

I am spending more than one day.... may be small issue ....

One activity call dialog & return to previous activity

Thanks in advance....


you are not passing any value from intent, so try to remove the intent and use like this:

setResult(RESULT_OK);


try this code..

     button.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 Intent intent= SalesRouteDevitionActivity.this.getIntent();
                 Log.i("*********" ," --- " +getParent());
                 if (getParent() == null) {
                     SalesRouteDevitionActivity.this.setResult(SalesRouteDevitionActivity.RESULT_OK, intent);
                 } else {
SalesRouteDevitionActivity.this.getParent().setResult(SalesRouteDevitionActivity.RESULT_OK, intent);
                }
                 finish();
             }
         });


getParent().setResult(RESULT_OK, intent);

This line is might be faluty, because getParent() returns the calling activity, actually if SalesRouteDevitionActivity was called from another activity X then THAT X activity is returned. But since that activity is in OnPause() and is listening for results then you cannot set the result from the same X activity at the same time, but set the result from SalesRouteDevitionActivity, and then finish it. Like

SalesRouteDevitionActivity.this.setResult(RESULT_OK, SalesRouteDevitionActivity.this.getIntent());
SalesRouteDevitionActivity.this.finish();

Also rewrite the onActvityResult(), by testing for request code then for result code...

   @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          Log.i("TEST ON RESULT", "SUCCESS");
          if( requestCode==2){

              if(resultCode==RESULT_OK)
                  Toast.makeText(this, "Reason has been successfully.", Toast.LENGTH_LONG).show();
             else
                  Toast.makeText(this, "Fail", Toast.LENGTH_LONG).show();
          }


I have done this from this link.

startActivityForResult from ActivityGroup? .Its very helpful for startActivityForResult from ActivityGroup


I've had a similar issue. I had an ActivityGroup managing sub-activities. One of the sub-activities called a similar external intent (external to my app). It never called the onActivityResult within the sub-activity that started it.

I finally figured out/remembered that the issue is because Android will only allow a nested layer of sub-activities...ie sub-activities can't nest sub-activitites. To solve this:

call getParent().startActivityForResult() from your sub-activity
your parent (the activitygroup) will be able to handle the onActivityResult. So I created a subclass of ActivityGroup and handled this onActivityResult.
You can re-route that result back to the sub-activity if you need to. Just get the current activity by getLocalActivityManager().getCurrentActivity() . My sub-activities inherit from a custom activity so I added a handleActivityResult(requestCode, resultCode, data) in that subclass for the ActivityGroup to call.

more information go here startActivityForResult from ActivityGroup?

0

精彩评论

暂无评论...
验证码 换一张
取 消