I have an android application with three activities the flow is like this
A->B->C
The problem is when i go from Activity C to Activity B. ( B<-C)
DETAILS AND BACKGROUND:
I Have an arraylist of of Albums that contain a list of photos, that contain a list of tags. Activity A deserializes the ArrayList to see if there are any new changes in the ArrayList. Activity A handles adding new albums, after adding albums I serialize the ArrayList, this works. I select an album which takes me to Activity B(Arraylist gets passed to activity B, successfully). In this Activity(B) I can add images. After adding images i serialize the Arraylist. This works, i can switch between activities A and B and the photos get saved. When I select the back button on activity B it takes me to Activity A, I do so by doing the following
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
case R.id.action_add:
.
.
.
case:
etc,
.
.
}
}
(EACH activity has the android back button in the top left hand corner)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In doing so I learned control flow goes into Oncreate() inside of Activity A as though a new instance is being created?
We are now inside of Activity B
Now If I select an image it takes me to Activity C.In this Activity(C) I can add multiple Tags. I serialize the ArrayList, then i click the back button taking me to Activity B, now that i am in activity B I click the same photo which then takes me to Activity C, but the tags are not displayed associated to the image, If i hard code tags they show up. But it seems as though the arraylist never gets updated. I don't know where control flow goes Back inside of Activity B, If i choose to take the path with OnCreate() A new instance gets created making my list object null
intent = getIntent();
arrayList = (ArrayList)intent.getSerializableExtra("key1");
becomes null, Since a new instance of Activity B gets created, getting invoked By C ,this ArrayList never gets passed to Activity B, because only Activity A calls B in that fashion right?
I want Activity C to be able to save the Arraylist properly, When i click the back button taking me to activity B, and re click the same photo i expect to see the tags pertaining to the image
Activity A:
Calls activity B by Intent
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key1", list); //list is an ArrayList
I pass a list to activity B
Activity B:
control flow then goes into Oncreate();
This method OnCreate() contains
intent = getIntent();
arrayList = (ArrayList)intent.getSerializableExtra("key1");
I receive the arraylist succesfully.
now I want to pass the list to Activity C
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
intent.putExtra("key2", list);
Activity C
{
control flow then goes into Oncreate();
This method OnCreate() contains
intent = getIntent();
arrayList = (ArrayList)intent.getSerializableExtra("key2");
now when i press the back button, i do so by doing this
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);//control goes to onCreate()??
case R.id.action_add:
.
.
.
case:
etc,
.
.
}
}
Now lets take away NavUtils.NavigateUpFromSameTask(this);
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case android.R.id.home:
//NavUtils.navigateUpFromSameTask(this);
finish();
case R.id.action_add:
.
.
.
case:
etc,
.
.
}
}
In doing so I get back to Activity B WITHOUT entering onCreate(), but if i click the photo the tags don't show up.
}
Still inside of activiy C
Related
I have two different activities, A & B.
Both have NavigationDrawer , look alike, but not are the same, because I could not get the drawer layout ID of activity A in activity B.
Both activity has 3 fragments each (total 6).
The NavigationDrawer contains all fragments of activity class A only. My problem is, when I am in activity B, and try to open one fragment of activity A from navigation drawer, it throws an error
No view found for id 0x7f090047 (com.wlodsgn.bunbunup:id/linear) for fragment FmMenu{b1e537f0 #0 id=0x7f090047}
How do I achieve it?
I have created an intent inside the second activity and started the activity A with information about the fragment to be called.
Intent i = new Intent(this, ActivityClass.class);
i.putExtra("frgToLoad", "FRAGMENT_A");
startActivity(i);
Now, inside activity A, checked the extra and load the right Fragment:
OnCreate(){
...
if (getIntent().getExtras() != null) {
String intentFragment = getIntent().getExtras().getString("frgToLoad");
switch (intentFragment){
case "FRAGMENT_A":
// Load corresponding fragment
break;
case "FRAGMENT_B":
// Load corresponding fragment
break;
case "FRAGMENT_C":
// Load corresponding fragment
break;
}
}
}
One have to check if intent is null or not before I try to assign a value to intentFragment. This is because that line of code is called whether I am coming from activity B or not, and it will throw error if intent is null.
credit : https://stackoverflow.com/a/36064344/3380537
I want to add back navigation to toolbar. I need to get from a fragment in an activity to a specific fragment in another activity.
It looks a little like this, where every orange line means navigating to a new activity or fragment:
How do I move from fragment B to fragment A from OtherActivity?
Consider these steps:
From Activity 1 holding Fragment A , you want to directly load Fragment B in Activity 2.
Now, I am thinking first, then you press a button in Fragment A, you can directly go to Activity B.
Then it means, you can simply load Fragment B as soon as you arrive in Activity 2.
Since you are dealing with back navigation (I believe you mean the upNavigation?), you can override the following:
But watch clearly, because if you need to load an exact fragment in Activity 2, you need to know somehow:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("frag", "fragmentB");
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
As you can see, when you click the back arrow on the toolbar, we pass a value through our intent to identity which fragment we want to load.
Next, in your Activity2, simply get the intent extra and do a switch or an if statement:
#Override
public void onResume(){
super.onResume();
Intent intent = getIntent();
String frag = intent.getExtras().getString("frag");
switch(frag){
case "fragmentB":
//here you can set Fragment B to your activity as usual;
fragmentManager.beginTransaction().replace(R.id.container_body, new FragmentB()).commit();
break;
}
}
From here, you should have your Fragment B showing in Activity 2.
Now you can handle the same thing while inside Activity 2 to decide where to go when a user clicks the back home arrow!
I hope this helps you get an idea.
Note: I thought about the interface approach and realized it is not necessary since this can be done easily with this approach!
To navigate from one Activity to another Activity's Fragment, with Kotlin version 1.4.0 and, for example, calling a click listener on a button it works so:
binding.yourButton.setOnClickListener {
supportFragmentManager.beginTransaction().replace(R.id.yourLayout, NameOfYourFragment()).commit()
}
Use this code to change your fragment
fragmentManager.beginTransaction().replace(R.id.container_body, new FragmentC()).commit();
and to show navigation on custom toolbar add
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true)
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
From Child Activity, I need to get next item in the ListView of the parent Activity.
Something like below should be perfect(not working, just exemple)
ParentActivity parent= (ParentActivity)getCallingActivity();
ArrayList yeahIGotReferenceToParentArray = parent.array;
Activity A= Listview with 30 row.
When user Click on a row, it open a new Activity B.
From Activity B, I have a button "Next", to get the next item of the ListView on activity A.
Possible solution:
I can use a Intent to provide the full array of the listView, but a better approach can Be to get the reference to the Activity A (from activity B), so I can access value.
You could pass that data, while you are still in activity A, to activity B through an intent. So in your onClick() method you could do something like...
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra("key", "value");
startActivity(i);
finish();
and in ActivityB
Bundle extras = getExtras();
if(extras != null){
if(extras.containsKey("key")){
//do something based on what the value of the data is
}
}
I don't know if this is the most efficient way of doing this but it could work!
I have three activites, lets call them A, B, and C.
I initially have activity A passing data with intent and calling activity B to open, displaying the data passed from activity A.
Now the issue is when I open activity C from activity B, and use the up navigation that I set up by setting activity B as the parent of Activity C in manifest, none of the data is displayed from Activity A in Activity B. If i simply make a button and call finish(); on the button instead, and not use the up navigation, the activity still contains all data from activity A just how I want it in Activity B.
I'm assuming this has to do with the lifecycle of using up navigation? I even tried using the intent from activity B to C by passing data, and then onResult have it return back to activity B, but it seems the onActivityResult is never called when up navigation is clicked. Any ideas? Maybe I can override this up navigation to just call finish(), like my button does, and nothing more?
Assuming you already have the parent activities defined at the Manifest, make sure you override the following methods:
#Override
public void onCreate(Bundle savedInstanceState) {
// You code here
getActionBar().setDisplayHomeAsUpEnabled(true);
// or getSupportActionBar().setDisplayHomeAsUpEnabled(true) if using support actionbar, i.e., for targets < 3.0;
}
Now you will be able to catch the action of the "Up" button with the Id android.R.id.home.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
I have two activities in my application. Activity1 has a list view and two buttons(say button1 and button2). Based on which button user clicks the content in the listview changes accordingly. The default loading of activity is having button1 click contents loaded in activity1.
In my Activity2, I have a button(say button3) which when clicked, has to load activity1 but with the listview loaded with button2 click results, not with the default display which shows the button1 click results.
Any help on how can I acheive this? On the onclick event of button3 in activity2 I can load activity1 but that will load with the default state showing button1 click results.
Save the state of your ListView by passing a extra string with IntentHandler when switching between the activities:
Intent intent = new Intent(getBaseContext(), yourActivity.class);
intent.putExtra("LISTVIEW_STATE", myListViewState);
startActivity(intent);
Then in your recieving activities onCreate() method (in this case yourActivity) you can get the state with:
Intent intent = getIntent();
String recievedListViewState = intent.getStringExtra("LISTVIEW_STATE");
From second activity send Intent with a flag.
In first activity always lookout for this flag and trigger your second button code accordingly. For reference you can search for "passing data between activity" here or google.
You can do this by adding Extras on your Intents. Right now, when you start Activity1 from Activity2, you probably have code that looks something like this:
Intent intent = new Intent(Activity2.this, Activity1.class);
startActivity(intent);
You can use the putExtra() method on the Intent that sets a key-value pair (called an extra) on this intent, making it look like this.
Intent intent = new Intent(Activity2.this, Activity1.class);
intent.putExtra("LIST_TO_DISPLAY", "LIST_2");
startActivity(intent);
The first argument to putExtra() (the key) is always a String, the second argument (the value) can be many different data types. Here's it's also a String.
Once Activity1 starts, you can grab the Intent by using getIntent() then pull the extra from it, all in onCreate().
protected void onCreate(Bundle b)
{
Intent intent = getIntent();
String whichList = intent.getStringExtra("LIST_TO_DISPLAY"); //which List now equals "LIST_2"
if( whichList != null && "LIST_2".equals(whichList) )
{
//Set up List 2
}
else
{
//Set up the default list
}
}
Here's the docs for the Intent Class. You can find explinations of all of the put and get Extra methods, as well as some other information that may be helpful when wanting to customize how you started Activities behave.
I dont understand so there are several things which would you think.
...
Activity1 -> Button1 -> Listview (Content A)
Activity1 -> Button2 -> ListView (Content B)
Activity1 -> Activity2 -> Button3 -> Activity1 -> ListView (Content B)
So you could
in activity1
start startActivity(intentActivity2);
In activity2
intentResult.putExtra("cmd", "button3");
onbutton click setResult(RESULT_OK,intentresult)
and finish
back in Activity1
onActivityResult
resultIntent.getExtra("cmd")