Complex custom button responsibility - android

I have created custom button that have two states: create_state and login_state.
In create_state, if pressed, it should switch to another activity where user can fill profile creation form, then go back to main activity, and switch to state login_state, so pressing button logon user.
I have done that in my ProfileButton class. And now I see that there are some problems.
For example when I use:
Intent goToNextActivity = new Intent(this.getContext(), NewProfileActivity.class);
Activity a = (Activity)this.getContext();
int requestCode = 0;
a.startActivityForResult(goToNextActivity, requestCode);
my void onActivityResult(int requestCode, int resultCode, Intent data) {..} declared in ProfileButton class does not start after second activity finish().
So better way is to manage this in parent Activity of that button?
All click handlers should be in Activity?

From my understanding, I guess you have to manually set the result that you need to return before finishing the activity.
use setResult() method in NewProfileActivity just before finish() method is called

Related

How check if my previous activity exist?

In my app, there are 3 activities and their calling sequence is like this...
Splash activity--->Dashboard Activity--->List Activity
If it goes like this then If I press back button on List activity then it will follow the reverse sequence of above.
There is one requirement where user can navigate to List Activity directly from Splash (Skipping Dashboard Activity) Now when user will hit back button on List Activity then I wan't to show Dashboard Activity which is not there in the Activity Stack.
So please help me with the best approach.
Pass a boolean through the intent for going to List Activity from either of the others. Using onBackPressed check if the boolean is true or false for skipping Dashboard Activity.
Then if true put new intent for loading dashboard activity and finish(); on list activity.
You have to pass class name as intent extra from both Splash and DashboardActiviy.
In List Activity you have to get the class name using getIntent().
When the user click back button, you need to check the class name based on that you can take decision.
if(name.equalIgnorecase(DashboardActivit.class.getSimpleName()){
//Add your intent
}else{
//
}
This may give you definite solution to you.Give a try
you can directly go to splash from list Activity while going to
ListActivity from Dashboard Activity call finish()
Intent i = new Intent(DashboardActivity.this,ListActivity);
startActivtiy(i);
finish();
Start your inner activities with startAcvitiyForResult
Intent i = new Intent(this, Activity_2.class);
startActivityForResult(i, 1);
and in your inner activity
#Override
public void onBackPressed ()
{
finish();
}
You can also do stuff in your outer activity as you like after your inner activity finishes
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == 1){
Log.i("TEST", "RESULT_OK");
}
else {
return;
}
}
}

Android back stack with multiple Activities

I have a few activities and can't figure out a way to make it work with the backstack.
Is there a way to do this:
MainActivity ->(intent) subActivity ->(intent)subsubActivity->(back press)subActivity->*(back press)MainActivity
*This is where I am having problems. Since I am coming from my subsubActivity, even thought I used android:noHistory="true" in the manifest, it doesn't go back to main activity.
Thanks for any help!
This is a perfect candidate for using a combination of startActivityForResult and overriding onActivityResult in the calling Activity.
Assuming you have Activity A, which starts Activity B (which we cannot move back to), which starts Activity C:
Activity A will call Activity B the way you are now. Activity B, however, will call startActivityForResult(Intent, int) instead of just startActivity(Intent). This way, when we return from Activity C, we can call finish() on Activity B and return to Activity A, like so:
public class ActivityB extends Activity {
private static final int REQUEST_CODE_ACTIVITY_C = 1001;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_CODE_ACTIVITY_C) finish(); // If coming back from Activity C, finish()
}
private void openActivityC(){
Intent intent = new Intent(this, ActivityC.class);
startActivityForResult(intent, REQUEST_CODE_ACTIVITY_C);
}
}
Now when you call openActivityC(), you're ensuring that onActivityResult() will be called when returning from Activity C, thus allowing you to end the Activity and return back to Activity A.
You can provide even more specific actions (such as setting/checking the result code (e.g. if it canceled (Activity.RESULT_CANCELED) or successful (Activity.RESULT_OK)) for statuses, to better determine what that calling Activity should do.
Hopefully that helps explain it a bit.
Edit: Also an afterthought, if there's no chance you'll ever want to go back to Activity B, then #Kay 's solution of just calling finish() after firing the Intent for Activity C would be the simplest approach.
If i get what you want You can use startActivities with an array of intents which will be fired one after another activity1->activity2->activity3 -back-activity2 -back-acitivty1

How to restart previous activity in android?

In my android app, I have an activity that displays profile information. Then it opens a new activity to make changes to the activity. After you save changes, it closes the edit activity from the edit activity, then goes back to the profile displaying activity, there I need to restart this activity to refresh the data.
Is there a way I can restart the activity that opened the current activity?
There are three ways you can achieve what you want:
Start the profile activity by calling startActivityForResult() and refresh your data in onActivityResult()
Finish() the activity when start the profile activity and then override onBackPress() in the profile activity to start the previous activity and then call finish().
Override onBackPress() in the profile activity to start the previous activity with the flag Intent.FLAG_ACTIVITY_CLEAR_TOP and refresh your data in onNewIntent()
Use Android Activity Result Pattern that communticates parent and child activities:
In your profile activity (parent):
#Override
public void onClick(View v) {
startActivityForResult(
new Intent(this, NameActivity.class),
1); // 1 to identify caller activity.
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
updateProfile();
}
}
In your editor activity (child):
#Override
public void onClick(View v) {
setResult(
RESULT_OK);
finish();
}
You won't have to "restart" Activity A.
You can call startActivityForResult(Intent, int) to start your new Activity, and then receive extras via the onActivityResult(int, int, Intent) call.
http://developer.android.com/reference/android/app/Activity.html#StartingActivities
Alternatively, look into the onResume() method, which is called every time your screen becomes visible. Assuming you persisted some state in Activity B (e.g. a SharedPreference or a database record), in onResume() you can refresh Activity A.
Activity Display;
Activity Modification;
When you startActivity M in your Activity D,don't D.finish();
then, when you M.finish(),you don't need "restart" Activity D,because it is already there.
You have two choices to "refresh the data":
1.startActivityForResult as they said.I won't explain any more.
2.make a cache.
save data in some static variables or sqlite or even sharedPreference.Then in onResume() of Activity D,get those data and show them.
Try this: If A is calling B and when B ends you need to update A, then in A launch B with startActivityForResult(). When B returns, the method onActivityResult() of A will be called. In this method (of A) you can test, if necessary, that some stored values are changed. At this point you do something like:
finish();
startActivity(intent); //start the activity A

OnActivityResult Doesn't work with activityGroup

im my App i use TabHost. and ActivityGroup to load activities under the tab. on my 2nd tab i open activityGroup "TabGroupActivity"... and from here i open a child activity "childActivity2". from the "childActivity2" i want to open an normal activity which has a theme dialog. and when i return from my normal activity i want to run the onActivityResult() in my childActivity2.
But the onActivityResult() in ChildActivity2 is not working.
the code where in childActivity2 to start the normal activity is
data.putInt("doctorId", doctor_id);
Intent createSchedule = new Intent(ScheduleWeekly.this, CreateSchedule.class).putExtras(data);
startActivityForResult(createSchedule, 1);
this is my onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==Activity.RESULT_OK)
{ Log.e("get","result");
.................
......
}
}
Your problem is same as mine. The problem is that the onActivityResult function won't directly trigger from a child activity in activity group, even your intent is from that child activity.
The solution is divided in three steps.
First, you have to let your parent activity, that is your ActiviyGroup class to call the startActivityForResult function in the position you need to jump out the current activity. In your child activity, when you need to lunch your normal activity, instead call:
startActivityForResult(intent, 0);
You should call:
getParent().startActivityForResult(intent,0);
This will let the ActivityGroup to take care of the call back. In your case, since your have three level nested, you may have to try whether the parent or grandparent should take care of the call back and make proper modification about getParent() part.
Second, after you make the parent class of current activity start the intent, you will need to add onAcivityResult() function into BOTH parent class and the current child class. In current class you just write normal call back handle message as you do now. But in parent class, the onActivityResult() function will catch the call back from the normal activity and deliver the intent to the current class.
Third, This step is for the parent onActivityResult class, in that class, your need:
public void onActivityResult(int requestCode, int resultcode, Intent data)
{
super.onActivityResult(requestCode, resultcode, data);
switch (resultcode)
{
case RESULT_OK:
MyChildActivity CA = (MyChildActivity) getLocalActivityManager().getCurrentActivity();
CA.onActivityResult(requestCode, resultcode, data);
}
}
As you can see, the onActivityResult function in parent ActivityGroup class is just catch the call back, get the child activity you which needs to jump to another activity, and transfer the data to it. You may not need exactly onActivityResult function in your child activity as state in Step 2, but I think this is a better way to do it.
Hope this help!

Have a main Activity that calls another Activity

My Activity calls another Activity during which , the app should move to other activity and should not affect the main Activity.Should the main activity be onPause() ?
and after the second Activity comes back to the Main Activity should it have OnResume()?
if you have any example will be great.
thanks for helping!!!
No rather you should use startActivityforResult(intent, requestCode) to start the next activity
and then override
protected void onActivityResult( final int requestCode,
final int resultCode,
final Intent data)
See Returning a Result from a Screen
That way you will know which activity was called before returning to main
If you are using Intents, there is no need for pause and resume, provided the flow of your activity is fine. Suppose you are going from A to B, all you do is mention the current activity name and the next activity where it should go.
For Ex:
Intent i = new Intent(ClassA.this,ClassB.class); where you are in class A and you are going to classB

Categories

Resources