Android: Nested tabactivities do not work with startActivityForResult

When I insert TabActivity inside another TabActivity, startActivityForResult fails when called from inside tabactivity. A new action begins, but an error message appears:

startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent 

and onActivityResult is never called when activity returns.

The code to play found below. Four classes, MyActivity is the main class with two tabs, NestedTab has three tabs, all tabs contain SimpleActivity with a button that calls SimpleDialog:

public class MyActivity extends TabActivity {
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Populate a couple of tabs
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, SimpleActivity.class);
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("simple").setIndicator("Simple")
                  .setContent(intent);
    tabHost.addTab(spec);

    // Repeat
    intent = new Intent().setClass(this, NestedTab.class);
    spec = tabHost.newTabSpec("nested").setIndicator("Nested tabs")
                  .setContent(intent);
    tabHost.addTab(spec);

 }
}

public class NestedTab extends TabActivity {
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent(this, SimpleActivity.class);

    String tabs[]= {"One", "Two", "Three"};
    for (String s : tabs)
    {
        intent.putExtra("name", s);
        spec = tabHost.newTabSpec(s).setIndicator(s).setContent(intent);
        tabHost.addTab(spec);
    }


}

public class SimpleActivity extends Activity {
Button mBtn;
Context mCtx;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple);
    mCtx = this;
    mBtn = (Button) findViewById(R.id.btn);
     mBtn.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
             //To change body of implemented methods use File | Settings | File Templates.
             startActivityForResult(new Intent(mCtx, SimpleDisplay.class), 1);
         }
     });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);    //To change body of overridden methods use File | Settings | File Templates.
    Toast.makeText(this, "Activity finished", Toast.LENGTH_LONG).show();
}
}


public class SimpleDisplay extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView text = new TextView(this);
    text.setText("Just hit back button");
    setContentView(text);

}
}

Unable to insert xml files, but the main one is identical to step 4: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

and simple - it's just a text view and a button.

Any help was appreciated.

+3
1

SimpleActivity.this.startActivity ((Activity) view.getContext()).startActivity startActivity. OnClickListener Activity , , Activity onActivityResult.

EDIT: , . , TabActivity - TabActivity, , , , .

, onActivityResult ; -, Intent ( , Intent, , ), , () Intent. , , - startActivityAndDispatchToChild, getParent().startActivityAndDispatchToChild. startActivityForResult getActivityResult , , , , . , getLocalActivity().getActivity(whateverTag) .

. , , , , Intent; , -, ViewGroup .

+3

Source: https://habr.com/ru/post/1778809/


All Articles