How to transition from activity 3rd to 1st? - android

I have a problem with Shared Element Activity Transition between activities. I have MainActivity, it has a recyclerview with boxes (recyclerview.horizontal). Each box when clicked will go to the corresponding activity. The problem that appears when I click on a box, I switch to the second activity, in the second activity I press a button to switch to the third activity. And here I swipe to right to return to the MainActivity with transition and I want it to transition right to the box corresponding to the 3rd activity in the recyclerview in MainActivity. So, my purpose is:
MainActivity (Shared Element Activity Transition)-> Second Activity ->
Third Activity (Shared Element Activity Transition)-> MainActivity
(exactly scroll to position for Third Activity in RecyclerView).
My MainActivity
I hope everyone offer me the solution. Thank you so much.

You can use startActivityForResult instead of startActivity in SecondActivity when you are going to start ThirdActivity.Like this :
Intent i = new Intent(this, ThirdActivity.class);
startActivityForResult(i, 1);
And when you are finishing your ThirdActivity
Intent returnIntent = new Intent();
returnIntent.putExtra("activity_finish",true);
setResult(Activity.RESULT_OK,returnIntent);
finish();
If you use startActivityForResult() then it gives callback in the Activity who started it,so as soon as ThirdActivity finishes, it will return to the onActvityResult() in SecondActivity.Where you have to check result_code and request code like this :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
boolean isActivityFinish=data.getBooleanExtra("activity_finish");
if(isActivityFinish){
// finish your Second Activity here
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
For more info : How to manage `startActivityForResult` on Android?

Related

How to choose which fragment to open from specific activity?

My mainActivity contains 5 fragments which you can switch through with tabs. The main tab shown on start is the third one (the one in the middle). I have a button in fragment #1 which opens activityTwo. What should I put in the onBackPressed method in activityTwo in order for it to bring me back to mainActivity with selected tab #1. I currently have this, but it opens mainActivity and shows the main tab (#3)
#Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
ActivityTwo.this.finish();
}
How can I make it show tab #1 instead of tab #3?
Your first activity should still be on the activity stack, so you should not call startActivity again. Instead, you can use setResult to pass a value back to your first activity:
// Inside your second activity
#Override
public void onBackPressed() {
setResult(RESULT_OK);
finish();
}
Then, from your first activity, replace startActivity with startActivityForResult, with a request code (any integer you want, as long as it's unique):
startActivityForResult(intent, YOUR_REQUEST_CODE);
Then override onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == YOUR_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Code to select tab 1 here
// mTabLayout.getTabAt(0).select();
}
}
}
i think you are using view pager , so while you return from activity 2, use viewPager.setCurrentItem(page);
where page will be your tab no, (in this case it will be 2, since it starts with 0). Hope this helps, if not then please clarify a bit more.

Sending data to Activity without startActivityForResult

How can I send data to another Activity that did not startActivityForResult?
I Override the onActivityResult method in order to perform logic as soon as a result returns from another Activity. Specifically, from my EditItemActivity
Process:
Within my MainActivity,
when I click on an item in the ListView, I am redirected to another Activity called ToDoDetailActivity
When I click the Edit button I am redirector to another Activity called EditItemActivity"
As soon as I make my changes and press the Edit button on the EditItemActivity, I return back to the MainActivity
data = new Intent(EditItemActivity.this, MainActivity.class);
data.putExtra(EDITTEXT_VALUE, etValue);
setResult(123, data);
startActivity(data);
Within my MainActivity, I want to UPDATE the item I just edited with this logic within my onActivityResult method. However, I don't see any logs in LogCat meaning I do not believe this method is being used and therefore no logic is performed
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {
return;
}
if (resultCode == 123) {
String editedItemValue = data.getStringExtra(EditItemActivity.EDITTEXT_VALUE);
todoItems.remove(editedPosition);
aToDoAdapter.insert(editedItemValue, editedPosition);
aToDoAdapter.notifyDataSetChanged();
}
}
In your code:
data = new Intent(EditItemActivity.this, MainActivity.class);
data.putExtra(EDITTEXT_VALUE, etValue);
setResult(123, data);
startActivity(data);
When you execute startActivity with MainActivity.class it is launching a new Activity so you are not going to find a result in "onActivityResult" method. It is because you are not returning to the main activity after finishing in a "child" activity.
What I have done in the past is use broadcast event to notify the "MainActivity" about updating the view.
Hope that helps.
We can use BroadcastReceiver or use database to store the result of your edit in EditItemActivity then update that result in onResume method of MainActivity

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

Complex custom button responsibility

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

stuck with getting camera pic when using the tab Activity

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,CAMERA_PIC_REQUEST);
Intent takePictureIntent = new Intent(getParent(),TakePicture.class);
takePictureIntent.putExtra("image",thumbnail);
OpenBeeActivityGroup opentActivity = (OpenBeeActivityGroup)getParent();
opentActivity.startChildActivity("TakePicture Activity",takePictureIntent);
As for I understand from your Question is,
This happen while using ActivityGroup. Since you are starting Activity for result inside a child Activity (i.e TakePicture.class), and Android will only allow single nested layer of child Activity(ies) (means child Activity cannot nest another child Activity).
And you are probably handling the result in your child Activity(i.e TakePicture.class).
So the solution to your problem is to handle that result inside your parent Activity (OpenBeeActivityGroup)'s onActivityResult() and then send your result to the active Activity.
you will use something like this.
inside your child Activity start your startActivityForResult() from parent Activity like.
getParent().startActivityForResult(cameraIntent,Global.CAMERA_PIC_REQUEST);
and inside your onActivityResult() of ActivityGroup (OpenBeeActivityGroup):
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == Activity.RESULT_OK)
{
switch(requestCode)
{
case Global.CAMERA_PIC_REQUEST: // global variable to indicate camera result
Activity activity = getLocalActivityManager().getCurrentActivity();
activity.onActivityResult(requestCode, resultCode, data);
break;
}
}
}
Along these lines, I have tried to start the camera using your code, and if you truly have it nested, then you cannot again call startActivityForResult. What you need to do is extend ActivityGroup to handle starting a child activity for result. I had to figure this out - HTH.

Categories

Resources