getParent returns null in TabActivity - android

I 'm calling getParent().setResult(0) in TabActivity. This is called when the user hits previous button. I want the current activity to close and get deleted from the stack.
getparent() returns null. Can someone tell me why does that happen??
getParent().setResult(0);
finish();
Thanks
UPDATE: This is the definition of getParent()...What does embedded child mean. And secondly is the TabActivity an embedded child if it is called from another Activity??
public final Activity getParent ()
Return the parent activity if this view is an embedded child.

You haven't clearly stated you're question. You want to understand how to pass a result back from an Activity to the Activity which called it. You must first understand that Activities aren't hierarchical even though they are kept on a back stack. Later activities do not belong to the Activity they are called from.
However, here is the answer the question you meant to ask:
You are using startActivityForResult(Intent, int) (Which you can read up on here)
When Activity A calls startActivityForResult() method Activity B is started, this should do whatever processing is required and then when it exits either call:
setResult(RESULT_OK)
when a yes/no is required or
setResult(RESULT_OK, intent)
where intent is an Intent which contains bundled information you want to use in Activity A to act upon.
After Activity B exits Activity A will resume and call the method:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
This is where you will process the result.
You can read all about this here:
http://developer.android.com/reference/android/app/Activity.html#StartingActivities

If you don't want to keep this Activity in the history stack do one of the following:
a) when starting the TabActivity in the parent, add flag: FLAG_ACTIVITY_NO_HISTORY
Intent intent = new Intent(this, TabActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(intent, requestCode);
b) OR add android:noHistory="true" in the activity's Manifest like so
<activity
android:name=".TabActivity"
android:noHistory="true"
...
>
...
</activity>
Then to return the result to the parent and finish TabActivity
Intent result = new Intent();
result.putExtra("somevalue", requestCode);
setResult(Activity.RESULT_OK, result); // or setResult(Activity.RESULT_CANCELED, result);
finish();
When you finish(); the TabActivity activity it will not be remembered

Related

Android how to start activity without intent or backround

Suppose I have two activities A and B activity A which contains a button I want to start Activity B when I press Button without intent.
According the Oficial Documentation:
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
So you have to use it to open activities with no exceptions or workarounds, if you do that, you are ignoring the entire system architecture.
There is no way to start an activity from anotherone without an intent.
If the reason of not using Intent that you don't want the the user to re-enter the previous activity
You can use finish() to finish that activity intent after you done work with
if(currentUser == null){
startActivity(new Intent(MainActivity.this,StartActivity.class));
finish();
}
So user will be unable to back again
If you want to do some code while the activity is finishing
You can use onDestroy() override method, Sometimes it can also be called if the activity is being killed by the android itself so you can add
isFinishing() function
Inside onDestroy() method which checks whether the application is closing by the call finish() returning true or otherwise by anything else returning false then you can easily specify your code for each situation.
#Override
protected void onDestroy() {
super.onDestroy();
if(isFinishing()){
// Activity is being destroyed by the function `finish()`
// What to do...
}else{
// Activity is being destroyed anonymously without `finish()`
// What to do...
}
}
Put your activity inside a Fragment and start the fragment fromo the button.
These are the possible ways to start any Activity
1st
startActivity(new Intent(Activity_A.this, Activity_B.class));
2nd
Intent intent = new Intent(Activity_A.this, Activity_B.class);
startActivity(intent);
3rd
Intent intent = new Intent(Activity_A.this, Activity_B.class);
startActivityForResult(intent,code);

Android onActivityResult called early

I have 2 Activities, each in seperate applications. Activity1 has a button the user can click and it calls the second activity using an intent in its onClick() method:
Intent myIntent = getPackageManager().getLaunchIntentForPackage(com.myProject.Activity2);
startActivityForResult(myIntent, 600);
This correctly launches Activity2 from Activity1, but onActivityResult gets called in Activity1 before onCreate gets called in Activity2, instead of in onBackPressed() where I set up the return intent.
Here is the onCreate method for Activity2:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Here is the current version of onBackPressed method for Activity2:
#Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("Stuff", someStuff);
if(getParent()==null){
setResult(Activity.RESULT_OK, intent);
}else{
getParent().setResult(Activity.RESULT_OK, intent);
}
finish();
super.onBackPressed();
}
My AndroidManifest.xml has the following intent filter for Activity2:
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
I verified that my launchMode is standard (and not singleTask, etc) as advised here and my request code is not negative as warned here. I also tried android:launchMode="singleTop", but that was a no-go also.
I also tried not calling finish() in onBackPressed() for Activity2 as mentioned here (also with just super.onBackPressed() as suggested here) and again calling it as suggested here.
Additionally I tried commenting out the line intent.putExtra("Stuff", someStuff); as it seemed to cause trouble for this person.
Any ideas as to what I might be doing wrong?
So here is the final solution that took care of it:
I changed the intent for Activity1 to the following:
Intent myIntent = new Intent();
myIntent.setClassName("com.myProject", "com.myProject.Activity2");
startActivityForResult(myIntent, 600);
For some reason Android requires the fully qualified name for the second parameter in addition to the package name given by the first parameter. Now it works! :)
It will happen if "singleInstance" flag is set when you launch the activity.
Not certain what your problem is. The way you're creating the Intent in Activity1 is odd; that method isn't meant for creating intents that launch another activity in the same app. Some developers use the Intent(Context, Class<>) constructor. I prefer to use Intent(String action) with a custom action string defined only in my app (which is easier to code correctly).
Also, the intent filter you've specified for Activity2 is usually used for an activity that's launched directly from the Home screen.
Where's the onCreate() code for activity2? Where's the code for onBackPressed()? Can you prove to me that setResult() is called before some other code in Activity2? You should run the activities in debug. Ensure that Activity2 is receiving the intent you think it should, then trace step by step the statements that are executed until setResult(). The thing not to do is throw solutions at the code before you understand what the underlying problem is.
As far as I can tell so far, Activity1 is sending out an Intent, and then onActivityResult is being called. Nothing else is proven so far.

How to close all child activities from parent in android [duplicate]

I'm new to android. Actually one handler is running in Home Activity A for every 30 sec to check the net connection.
If I'm went to activity C by A->B->C, If there in no net connection at that time, then i want to close Activity B and C, then want to show message box in Activity A.
But My problem is My handler is running for every 30 sec in Home Activity A. But If i was in Activity C or some other Activity how to find which activity is my Application currently focussed now. Then i want to finish those child activities and want to show Home Activity A I have some 9 child activities in Activity B.
I heard about using "FLAG_ACTIVITY_CLEAR_TOP" . I used the code as follows in the handler in Home activity A itself. But got error.
Intent intent = new Intent( ctx, Homepage.class );
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
ctx.startActivity( intent );
Here Homepage.class is my Home Activity A and i set that activity in manifest file as
android:launchMode="singleTop"
Please help!
You can start Activity A and close all other activities.
You have to create new intent and add flag FLAG_ACTIVITY_CLEAR_TOP
Intent activityA = new Intent(context, ActivityA.class);
activityA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.StartActivity(activityA);
this will close all activities that are in the stack and are at the top of activity A
When calling Acitivity C, call finish in activity B , when calling Activity A from C , call finish() in activity C !
You can use, According to me two ways,
If you start an activity using startActivityForResult, then you can call finish() in this new Activity when you're done with it and it will return control to the activity that started it.
OR otherwise, May be I am wrong,
Call finish inside onStop 'override method'.
make a uniform resultCode for closing child activities. Eg. you make 911 (should be int) as your resultCode. If you want your Activity to finish and go back directly to parent Activity, you set the resultCode to 911:
setResult(911); finish();
In every child activity, you override the onActivityResult and check if the resultCode is 911. If yes, then call the setResult(911); finish(); until you get back to your parent activity. Hope this helps!
For example, if you want to start intentB, you can do following in old activity:
Intent intentB = new Intent();
intentB.setClass(XYZ.this, abc.class);
intentB.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentB);

How to finish all child activities from parent in android [duplicate]

I'm new to android. Actually one handler is running in Home Activity A for every 30 sec to check the net connection.
If I'm went to activity C by A->B->C, If there in no net connection at that time, then i want to close Activity B and C, then want to show message box in Activity A.
But My problem is My handler is running for every 30 sec in Home Activity A. But If i was in Activity C or some other Activity how to find which activity is my Application currently focussed now. Then i want to finish those child activities and want to show Home Activity A I have some 9 child activities in Activity B.
I heard about using "FLAG_ACTIVITY_CLEAR_TOP" . I used the code as follows in the handler in Home activity A itself. But got error.
Intent intent = new Intent( ctx, Homepage.class );
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
ctx.startActivity( intent );
Here Homepage.class is my Home Activity A and i set that activity in manifest file as
android:launchMode="singleTop"
Please help!
You can start Activity A and close all other activities.
You have to create new intent and add flag FLAG_ACTIVITY_CLEAR_TOP
Intent activityA = new Intent(context, ActivityA.class);
activityA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.StartActivity(activityA);
this will close all activities that are in the stack and are at the top of activity A
When calling Acitivity C, call finish in activity B , when calling Activity A from C , call finish() in activity C !
You can use, According to me two ways,
If you start an activity using startActivityForResult, then you can call finish() in this new Activity when you're done with it and it will return control to the activity that started it.
OR otherwise, May be I am wrong,
Call finish inside onStop 'override method'.
make a uniform resultCode for closing child activities. Eg. you make 911 (should be int) as your resultCode. If you want your Activity to finish and go back directly to parent Activity, you set the resultCode to 911:
setResult(911); finish();
In every child activity, you override the onActivityResult and check if the resultCode is 911. If yes, then call the setResult(911); finish(); until you get back to your parent activity. Hope this helps!
For example, if you want to start intentB, you can do following in old activity:
Intent intentB = new Intent();
intentB.setClass(XYZ.this, abc.class);
intentB.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentB);

How can I receive CameraActivities result in a DIFFERENT Activity (i.e. not in the launching one)?

I've got Activity A which fires up the Camera intent via:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
After the picture is taken I can easily grab the picture in:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
But I'd like to receive the result in Activity B in which the image can be edited.
Right now I'm receiving the result in Activity A and pass it over to Activity B which results in showing the GUI of Activity A for a short while:
Intent i = new Intent().setAction("DisplayJPEG");
i.setClass(this, EditImageActivity.class);
i.putExtra("IMAGE_URI", uri);
startActivityForResult(i, REQUEST_EDIT_IMAGE);
Of course, I will need the result from Activity B in Activity A after the image has been edited. But that should work with:
setResult(resultCode, data);
So there has to be a way to do what I need. Please point me into the right direction.
Have you tried launching ActivityB, and in ActivityB onCreate event launch the Camera Intent?
You technically can't do what you're asking. You'll need to find a way to continue passing it the way you are and hide the UI or do as Pentium says and do it the other way around.
Edit: Nevermind, I misread how this works. What actually happens is you can use Activity A to start Activity B for result, but then if Activity B needs to start Activity C to continue processing whatever Activity A wanted, you can use FLAG_ACTIVITY_FORWARD_RESULT to make Activity C return its result to Activity A not B.
I haven't looked into this more than a quick glance, but I noticed an Intent flag called FLAG_ACTIVITY_FORWARD_RESULT which according to the documentation:
If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity. This way the new activity can call setResult(int) and have that result sent back to the reply target of the original activity.
Like I said, I haven't experimented with this, but that seems to suggest that you could launch your camera intent from Activity A but have it forward its result to Activity B.

Categories

Resources