1

いくつかのタブで TabActivity を使用し、各タブには ActvityGroup が含まれ、各 ActivityGroup は複数の Activity.1 を管理します。

最初にAが作成され、ユーザーがAのボタンをクリックすると、Bにジャンプします。

BにはCで編集できる重要なデータがいくつかあり、Bの「編集」ボタンをクリックするとCにジャンプします。

一部のデータがCで編集されている場合、戻るボタンをクリックすると、Bで同じデータを変更したい.クラスCで「finish()」を使用すると、アプリが直接終了します。

私はネット上で多くの解決策を検索しましたが、どれも私のケースには合いません。どこが間違っているのかわかりません。助けてください。または、ActivityGroup で startActivityForResult() を使用する方法の例を教えてください。

ここに私のグループがあります:

public class MyGroup extends ActivityGroup 
{

private int ID=0;
private AlertDialog dialog;
private Stack<View>history;                
private LocalActivityManager manager;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    history = new Stack<View>();
    manager = getLocalActivityManager();

    Intent intent = new Intent(this,A.class);
    startActivity(intent);
}

@Override
public void startActivity(Intent intent) 
{
    View view = manager.startActivity(""+ID++,intent).getDecorView();
    history.push(view);
    setContentView(view);
}

@Override
public void startActivityForResult(Intent intent,int requestCode)
{
    // super.startActivityForResult(intent, requestCode);
    View view = manager.startActivity(""+ID++,intent).getDecorView();
    history.push(view);
    setContentView(view);
}

/*
 * if user edited data in C.class.
 * when C.class finished,refresh data in the B.class
 */
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
    Log.e("MyGroup","running");
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK)
    {
           //modify data in B.java
        B subActivity=(B)(manager.getCurrentActivity());
        subActivity.handleActivityResult(requestCode, resultCode, data);
    }
}

/*
 * when press back button, manage which page to show next
 * if there is one page in stack,that means when press back button it will
 * exit the app,so we add a dialog to notify user whether exit app or not
 */
@Override
public void onBackPressed()
{
    int size=history.size();
    if ( history.size()>= 2) 
    {
        history.remove(size-1);
        setContentView(history.get(size-2));
    }
    else
    {
        if(dialog==null)
        {
            createDialog();
        }
        dialog.show();
    }   
}  
}  

B.class:

     public void nextPage()
     {
      Intent intent=new Intent(B.this,C.class);
      intent.putExtra("name", productAdapter.getName(position));
      intent.putExtra("id", productAdapter.getID(position));    
      B.this.getParent().startActivityForResult(intent,11);
     }

    /*
     * modify data in modifyItem
     */
      public void handleActivityResult(int requestCode,int resultCode,Intent data)
     {
      String price=data.getExtras().getString("price");
      String name=data.getExtras().getString("name");
      String quantity=data.getExtras().getString("quantity");
      productAdapter.setName(name, modifyItem);
      productAdapter.setPrice(price, modifyItem);
      productAdapter.setQuantity(quantity, modifyItem);
      productAdapter.notifyDataSetChanged();
   }

Cクラスで:

@Override
public void onBackPressed() 
{
    if(price!=null)
    {
        Bundle bundle=new Bundle();
        bundle.putString("name",name);
        bundle.putString("price",price);
        bundle.putString("quantity",quantity);

        this.getParent().setResult(RESULT_OK,new Intent().putExtras(bundle));
        this.finish();
        Log.e("C","inner");
    }
    Log.e("C","outer");
    this.getParent().onBackPressed();
}
4

2 に答える 2

0

ActivityGroup からのアクティビティは、応答呼び出しを直接取得しません。ActivityGroup からリダイレクトする必要があります。以下の回答を参照してください。これが解決策です。これを試してください

https://stackoverflow.com/a/15047518/1403112

于 2014-09-17T12:06:55.287 に答える