calling requestWindowFeature() after onCreate() - android

As mentioned in several answers, calling requestWindowFeature(Window.FEATURE_NO_TITLE) should be before super.onCreate(...) and setContentView(...).
However, I want the screen's title to appear when the activity is created, and to disappear only after returning from another activity.
I tried this:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
And I'm getting the android.util.AndroidRuntimeException: requestFeature() must be called before adding content exception.

// 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);
}
}

Related

Communication to activity in stack

Let's consider the following situation:
Activity A opens Activity B. Now, A is in activity stack. B downloads any data from the Internet and, basing on that data, we conclude that when user come back to the A ( after press back) A should refresh its content. How to say: B: Hey A in stack, please remember that you should refresh your content. I see that I can set some flag in App instance, but, it seems to be weird.
Consider using startActivityForResult in your ActivityA to call ActivityB, then within your ActivityB, override onBackPressed() method and call setResult() based on downloaded data. Finally back into your ActivityA override onActivityResult(int requestCode, int resultCode, Intent data)
Use the following example as guide:
ActivityA.java
public class ActivityA extends AppCompatActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, 1234);
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, Intent intent) {
if (1234 == requestCode) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Do action 1", Toast.LENGTH_SHORT).show();
}
if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Do action 2", Toast.LENGTH_SHORT).show();
}
}
}
}
ActivityB.java
public class ActivityB extends AppCompatActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onBackPressed() {
final Intent intent = new Intent();
if (true) { //Replace this condition by yours based on downloaded data
setResult(Activity.RESULT_OK, intent);
} else {
setResult(Activity.RESULT_CANCELED, intent);
}
super.onBackPressed();
}
}
Not that way. Always update Activity A content in the onResume() method of A activity.

onActivityResult doesn't get called by finish() when the parent activity is passed as parameter to an intent in a non-activity class

I've got a class which handles a question sequence. It doesn't extend Activity. In the class there is the method:
public class QuizMaster {
public void startQuiz(Activity activity, Model model) {
//switch - case statement using model
Intent intent = new Intent(activity, QuestionTextActivity.class)
activity.startActivityForResult(intent, requestCode);
//other case statements with other intents
}
}
When I call this method from a working activity with
mQuizMaster.startQuiz(this, mModel);
And I finish() the child activity:
Intent returnIntent = new Intent();
returnIntent.putExtra(ARG_SELECTED_CHECKBOX, checkedBox);
setResult(RESULT_CODE, returnIntent);
finish();
it doesn't execute the parent activity's
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
Log.d(LOG_TAG, "OnActivityResult called in SignDetailsActivity. Resultcode is: ");
}
But when I execute the
Intent intent = new Intent(activity, QuestionTextActivity.class)
activity.startActivityForResult(intent, requestCode);
in the actual parent activity file, it does execute the onActivityResult method.
Why doesn't the child activity run the onActivityResult in the parent activity if sent with a non-activity class? How do i fix this?
I haven't found anyone with the same problem with executing new Intent() in a non-activity class like this. If there is someone, i didn't use the right search keywords and some others might type in the same as I did and come on this page.
You need to call setResult(int) before call finish(). This is from Activity documentation:
When an activity exits, it can call setResult(int) to return data back
to its parent. It must always supply a result code, which can be the
standard results RESULT_CANCELED, RESULT_OK, or any custom values
starting at RESULT_FIRST_USER. In addition, it can optionally return
back an Intent containing any additional data it wants. All of this
information appears back on the parent's Activity.onActivityResult(),
along with the integer identifier it originally supplied.
Here is my implementation, which worked:
MainActivity.java (parent activity)
public class MainActivity extends AppCompatActivity {
private Sample sample;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sample = new Sample();
sample.startActivity(MainActivity.this);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("TEST", "DONE");
}
}
LaunchActivity.java (child activity)
public class LaunchActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
Button btn = (Button) findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setIntent(new Intent());
finish();
}
});
}
}
Sample.java (class start activity)
public class Sample {
public Sample () {}
public void startActivity (Activity a) {
Intent it = new Intent(a, LaunchActivity.class);
a.startActivityForResult(it, 0);
}
}

onActivityResult not being called when launching another app

I am trying to call another app through intent, the app is called but the onActivityResult is not being called. Could someone please help me on this?
Below is my code:
public class EncryptCommandActivity extends Activity{
EncryptionFactory encryptionFactory = new EncryptionFactory();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.encrpyt_command_activity);
ActivityContexts.setEncryptCommandActivityContext(this);
Intent intent = new Intent("asd.com.qweapi.MAIN_ACTIVITY");
Bundle bundle = new Bundle();
bundle.putInt("Function", 1006);
bundle.putString("MSG", MQTTFactory.getById());
intent.putExtras(bundle);
startActivityForResult(intent, 0);
finish();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent pData)
{
super.onActivityResult(requestCode,resultCode,pData);
Log.d("Encrypt","Inside"); //not called
Toast.makeText(ActivityContexts.getMainActivityContext(),"encrypt", Toast.LENGTH_LONG).show(); //not called
}
}
You should remove finish() in onCreate() because it will finish activity and then it's no longer to exist, since can not fire onActivityResult()
move finish() to onActivityResult from onCreate

onActivityResult not being called after FragmentActivity

I have an activity A that calls an activity B (FragmentActivity) using startActivityForResult(). Before activity B finishes by calling finish(), I use setResult(). In spite of all of this, onActivityResult() is not called. I'm not sure what I'm doing wrong as I'm not doing anything unconventional, or if this is an Android bug.
Seeing some of the suggestions on here, I've checked to see that I don't have android:launchMode="singleInstance" nor android:noHistory="true" set anywhere in my manifest file.
Calling activity B from A:
Intent intent = new Intent(getActivity(), ActivityB.class);
startActivityForResult(intent, MY_RESULT);
Finishing activity B:
Intent intent = new Intent();
setResult(Activity.RESULT_OK, intent);
finish();
Overriding onActivityResult() in activity A:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
Toast.makeText(getApplication(), "toast", Toast.LENGTH_LONG).show();
}
Edit 1
I commented out nearly all of activity B's code (see below), leaving A's intact and the problem remained. So, A is definitely doing something strange that causes the problem. Worse, I have done this with other activities (using the same code) without any problems! A extends ExpandableListActivity, although I don't understand how any of that code would cause issues in onActivityResult(). Logcat isn't giving me anything useful.
This is the code tested for activity B, for posterity's sake (though it seems to be that the problem is in A):
public class ActivityB extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activityb);
Intent intent = new Intent();
intent.putExtra("data", "Hello from ActivityB");
setResult(Activity.RESULT_OK, intent);
finish();
}
}
try this and verify with your code:
ActivityA.java
public class ActivityA extends Activity{
private static final int MY_RESULT = 0;
private Button btnStart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
btnStart=(Button)findViewById(R.id.btnstart);
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, MY_RESULT);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(getApplication(), data.getStringExtra("data"), Toast.LENGTH_LONG).show();
}
}
ActivityB.java
public class ActivityB extends FragmentActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Intent intent = new Intent();
intent.putExtra("data", "Hello from ActivityB");
setResult(Activity.RESULT_OK, intent);
finish();
}
}
Note : make sure you have added both the activity in your manifest file.
OK, thanks to everyone that pointed me in the right direction. I found the solution. The issue was that I was doing startActivityForResult() in a DialogFragment so I should have done
getActivity().startActivityForResult()
instead of just
startActivityForResult()
then it works as expected.

Why onStop() is called after onCreate() when starting an Activity within onActivityResult()

I have the following workflow:
startActivityForResult(Activity1)
finish() called on Activity1 (when pushing a button)
onActivityResult() ==> startActivityForResult(Activity2)
===> Activity2.onCreate() is called before Activity1.onStop()
Why I have that?
Edited:
Here is the code:
1- MainActivity.java
// On click on a button
public void start(View view) {
Intent activityIntent = new Intent(this, Activity2.class);
startActivityForResult(activityIntent, 0);
}
protected void onActivityResult(int requestCode,
int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Intent activityIntent = new Intent(this, Activity3.class);
startActivityForResult(activityIntent, 0);
}
2- Activity2.java
// A button to finish the activity
public void stop(View view) {
finish();
}
#Override
protected void onStop() {
super.onStop();
}
3- Activity3.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity3);
}
Because of the lifecycle. onStop isn't called until after an Activity is removed from view. So onStop won't be called until something else is blocking it from the user- activity2 in this case. That means Activity2 will already have to have been created, because you can't block another activity if you don't exist.

Categories

Resources