issue with second activity onActivityResult - android

I have my code of first activity:
and a second activity:
but I am not sure why the part "onActivityResult" is not functioning to show the gallery?

You are starting Gallery Activity incorrectly in on Click callback in first Activity. In order to start one Activity from the other, you should use startActivity method, instead of instantiating object:
startActivity(new Intent(this, GalleryActivity.class));

Related

back to first activity from each activity and recreate first activity

I have 3 activity, I want to return to the first activity and Refresh it after clicking on the button at second and Third activity, without using "new Intent(...)"!
I use this code:
ActivityFirst actFirst = (ActivityFirst) _context;
actFirst.recreate();
but it doesn't work! with Error : java.lang.ClassCastException
Is there any way to refresh first activity from second or third activity without use below code?
Intent intent = new Intent(this, ActivityFirst.class);
startActivity(intent);
You cannot refresh your first Activity because the thread is paused when the Activity is not in the foreground. Also the reason you are getting a ClassCastException I'm assuming is because your trying to cast your second or third Activity into the first Activity by passing your current Context.
You should just refresh your first Activity inside onResume like so:
#Override
protected void onResume() {
super.onResume();
recreate();
}
To start your first Activity again you could call this.
Intent intent = new Intent(this, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
If you want to finish second or third activity when you click on button on these activity then just use onActivityResult in First Activity and refresh you first activity.
If you only want to refresh without finish second ot third activity then send broadcast message to first activity.
Let me know this is useful for you?
Go through Activity Life Cycle, When you have opened the 2nd activity the first activity will be in pause state.so while resuming the activity you can refresh the activity.Try these method to implement it : onResume()onPause()
startActivity(new Intent(B.this,A.class));
finish();
If you pass Intent from second activity(B.this) to first activity(A.class) ; it will be recreate that first activity. When you press back than not second activity open because of finish();.

start activity inside fragment without button click. Android

How do i start an activity inside fragment without button click?
I want to directly start a activity inside fragment without clicking the button.
I have searched for answers but every answer is related to button click.
Please help. I am new to android.
override oncreateView() method and in that
Intent i=new Intent(getActivity(),yourActivity.class);
startActivity(i);
Let me clear about Activity & Fragment. Activity is basic android component which can hold many Fragment. Fragment can not hold any Activity. For example : MyActivity is an activity which may hold MyFragment1 & MyFragment2. Now you can go from Fragment to another activity not MyActivity because MyActivity is already running & holding fragments. If you want to go MyActivity2 from fragment then write simple activity starting code. I am giving it bellow.
Intent intent = new Intent(getActivity(),MyActivity2.class);
startActivity(intent);
You can write it onCreateView of fragment.

startActivityForResult annoying behaviour

I have the following case
When press a button I startActivityForResult , it opens another activity with dialog theme and show a list of linear layouts, and when I press on the linear layout I finish the activity on the onClick Listener
but the onActivityResult method never called
so can anyone help in this ??
EDIT: Adding code
Activity that open the dialog activity
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra(DATA_LIST_KEY, myDataList);
startActivityForResult(intent, 5000);
onClick Listener
public void onClick(View v) {
setResult(5000);
SecondActivity.this.finish();
}
EDIT 2
I have found the following
1- I'm using TabActivity to add tabs to all my activities, now The onActivityResult fire on the TabActivity but doesn't fire in the sub activities inside it
Can any one help ??
Make sure you call setResult(intent) method in the second activity after clicking the item.
Edit 1:
I think you set the result in a wrong way. It should be an intent with either Result_OK or Reuslt_Cancel.
Edit 2- Example:
Intent output = new Intent();
output.putExtra(key, val);
setResult(RESULT_OK, output)
Hints to check:
You call setResult before finish().
Remove android:launchMode="singleInstance" or android:noHistory for your activity if present
I have figure out the reason of this problem, I was using TabActivity and using activities as tabs and the onActivityResult called but in the parent activity that hold all other activities and I have solved it by handling it in the parent activity

From within an android activity test, how do I finish another activity that my main activity started?

I am writing an activity test for an activity we wrote with 3 buttons. 2 of these buttons start other activities.
I can write a test that simulates a button push and then checks if the desired activity is running, but I can't move back from that second activity. The second activity stays at the front and prevents the other tests, that assume the first activity is running, from working properly. They just kind of freeze.
I have a reference to the first activity, but it is the second activity I need to I guess call finish() on. Is there a way to do this?
EDIT: I added some actual source code illustrating my problem in this gist: https://gist.github.com/3076103
It is specifically about testing activities. In the production code everything is fine.
You should probably use http://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor.html to get a reference of the second activity or you can block the second activity from being launched(Still you are guranteed that the call to start the second activity infact had reached till the framework).
You need a way for your activities to communicate with one another, so that one activity can tell the other to finish. There are several ways you can accomplish this. One method is to create a service within my application; my "second" activities would connect to this service to register a way to receive messages, and my primary activity would connect in order to provide them.
In Activity1 add the following to start Activity2
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
In Activity2 add the following to start Activity1 and finish Activity2
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
For more details: http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/
If you start the activity using startActivityForResult, then you can close the activity from parent using finishActivity(int requestCode).
To start the activity :
startActivityForResult(new Intent(...), 123123 /*requestCode*/);
And when you want to finish that activity (from caller), use :
finishActivity(123123 /*requestCode*/)
Also there is a way to find, whether child activity is finished or not. But you can track this only when child activity calls finish() for self. To receive the child finish request from the child, you need to override the finishFromChild() method in parent activity.

how to avoid duplication screen in android

I have did a application in android by calling sub activities(B,C,D,E) from main activity(A),again calling that main Activity(A) from other sub Activities(B,C,D,E)by using
Intent in = new Intent();
in.setClass(getApplicationContext(), maindashbord.class);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(in);
when i move back from main Activity to login page by clicking back button ,the main Activity displays again and again the times of I have called the subactivity. I have made all other activities in manifest.xml Android:noHistory="true"
but I need some static value in mainActivity for next login propose...
how can I replace the duplicate screen display?
thanks in advance..
After you call startActivity you can call finish() on the next line to stop the current activity.
To store a value even after the activity is, either use Shared Preferences or Extend the appliation class, which you can access from any activity using the getApplicationContext(). This will return a singleton, so the value will be the same in all activities.
try flag FLAG_ACTIVITY_NEW_TASK instead of FLAG_ACTIVITY_CLEAR_TOP.

Categories

Resources