在onCreate()之后调用requestWindowFeature()

时间:2014-05-10 09:50:50

标签: android

正如多个answers中所述,调用requestWindowFeature(Window.FEATURE_NO_TITLE)应该在super.onCreate(...)setContentView(...)之前。 但是,我希望在创建活动时显示屏幕标题,并且只有在 从其他活动返回后才会消失。 我试过这个:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case REQUEST_CONNECT_DEVICE: 
            requestWindowFeature(Window.FEATURE_NO_TITLE);
    }
}

我收到android.util.AndroidRuntimeException: requestFeature() must be called before adding content例外。

1 个答案:

答案 0 :(得分:1)

// try this way,hope this will help you....

Note : i think what you trying do is not possible so try this alternative.
public class FirstActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if(!getIntent().getBooleanExtra("isTitleShow",true)){
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // this condition for stop to call SecondActivity after one time call
        if(getIntent().getBooleanExtra("isTitleShow",true)){
            Intent intent = new Intent(this,SecondActivity.class);
            startActivity(intent);
            finish();
        }
    }
} 


public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this,MainActivity.class);
        intent.putExtra("isTitleShow",false);
        startActivity(intent);
    }
}