How get the calling activity of an intent? - android

I have 2 list views :
- The first one is instantiated in a fragment
- The second one in a activity.
They display the same information
On click on a list item, I open another activity to display item detail.
Depending on the calling activity/fragment, I want to render a different layout, so I need to know in the activity that render the detail of a item, which activity created the intent ?
getCallingActivity and getParentActivityIntent seems to be declarative and fixe, isn"t it ? in my case there are both null.
How can I do that ?

You can pass an argument with your intent as:
intent.putExtra("ParentActivity", "ActivityA");
or
intent.putExtra("ParentActivity", "ActivityB");
In your next activity, use this:
String parentActivity = getIntent().getStringExtra("ParentActivity");
And render the layout according to result.
Hope it helps.

Related

First Activity to Second Activity (Intent, Database and Adapter)

I have a database and adapter for the first activity showing the name and description. There is a button on each list item which takes you to the second activity displaying a unique image related to that item.
I have included an Intent from the first activity to the second activity.
So on the second activity I would like to add the image related to the item clicked.
Question:
(a)Do I include the image in the same database for the first activity or do I need a separate database and adapter for the second activity?
(b)Also do I need to create a separate intent for each item in the first activity as each item has a separate image that it will link to via the button which will be displayed on the second activity.
Your click listener will be one and generic as same lite item view is inflated for all items of adapter.
2.on click you need to pass the Uri string to second activity via intent and display the image Uri in second activity after receiving it from getIntent() in oncreateview() of second activity.
I would like to answer this in two parts.
Part 1: the database: You should use the same table for the image as well. You can always talk to the same database and all the tables from any activity, so no need to have a separate database. So the name, description and image in the same activity.
Part 2: The intent
If you are in a scenario where you have to add any action on click of an adapter item, always use a callback.
When you click on any item, this callback will tell you in the activity that which item in the adapter is clicked.
This medium blog is a very good example to demonstrate this.
In this blog's code, there is a block in adapter where you pass the values from the activity. It is the constructor.
RecyclerViewAdapter(RecyclerViewClickListener listener) {
mListener = listener;
}
If you had added some code, it would help, but I am sure you also have this constructor in your code, so add this listener along with the other data and see how it works out.
Thanks

startActivityOnResult() in recycler view onitemClick()

so I need to create a notes app, in which user opens and sees a RecyclerView-grid of notes. on clicking any note, it opens a new activity SecondActivity with 2 edit texts title and data.
On closing secondActivity, I want the user to see the title he had set in secondActivity on the grid he clicked on the main page.HOW?
After searching for a white, i got this startActivityForResult(). i understood its working, but it is not available in the adapter/holder, where my onitemclicklistener is present.
So how can i get to recieve this title? can I use the saveInstanceState bundle for this work?I also researched and observed that a child Acticvity's(SecondActivity) onSaveInstanceState is not called when the secondActivity is destroyed. so maybe that bundle thing would be a wrong path.In my OnBindHolder() function, i was using myholder.itemview.getContext().startActivity(...,...) for calling the secondActivity. I also tried passing the context for main Activity in adapter and using it for starting activity, but it still didn't show startActivityForResult() ...
UPDATE:
here are the java classes from my app(I have explained in detail about my problem in a comment in Main Activity.java):
MainActivity.java
Details.java
(Reycler View Files:)
RVadapter.java
RVholder.java
RVdata.java
RVfeeder.java
githubLink
You need to cast Context to Activity because it has startActivityForResult.
((Activity) context).startActivityForResult()
or
(((Activity) itemView.getContext()).startActivityForResult()

Launching Fragment from activity

In my MainActivity, I have a viewPager
containing two containers "A and B".
Both containers are possessed with their own Fragments.
In container B; I again have a viewPager containing multiple fragments.(For example one,two,three,four)
On launch of my app MainActivity gets load with container A's homeFragment.
What I need to do is,For a given condition I have to launch fragment two of container B from onResume of my MainActivity.
What I've able to achieve is , I have successfully redirect to container B's fragment one but couldn't able to redirect towards fragment two from onResume of MainActivity, any help will be appreciate.
If you are using Intent to redirect to Fragment B, then pass the int position of sub fragment, and on the fragment B side check for intent extras and set the current page, which you received via intent.getIntExtra().
If I understood it clear, you want to show the second page of the viewpager when you open it, if so you can use mViewPager.setCurrentItem(1, true);
I think you want to set the specific page .. so to do that you can set viewpager as :
viewpager.setCurrentItem(item, true);
Certainly,I found a way of achieving this by using static concept.
I have declared a static variable inside container B's fragment two,and set its value in onResume of MainActivity.
When onResume calls with given condition it sets static variables value to the required fragment, and in my container B,I have used this value to set setCurrentItem() of B'viewPager.
ps: I know it quite confusing as it seems but not too complex in practice,work fine.

How to get value from another activity and bring back to previous fragment?

I have an Activity that have 3 fragment(FragmentA, FragmentB, FragmentC) like sliding tab. From FragmentB call another activity (lets call ActivityBB). After get item from Activity BB, How I can get value from ActivityBB and bring back to previous FragmentB ???
Well there are three ways that just comes in my mind. There may be more. But for now let me tell you those.
On ActivityBB put the values that you want to save in SharedPreferences. And then restart your activity. Well yes this might only work if you have values that can be arranged in key-value pairs. And is also not the proper way to do things. But will get your job done.
To restart an activity use this code. and then get your values from SharedPreferences.
Intent intent = getIntent();
finish();
startActivity(intent);
You can implement interfaces. This method is the best way to communicate between fragments. For more details check the google's documentation.
http://developer.android.com/training/basics/fragments/communicating.html
You can use Bundles. For that check this link.
How to pass a value from one Fragment to another in Android?
You can try some like this..
Pass your value into intent.
This code in your ActivityBB
Intent intent = new Intent(ActivityBB.this,ActivityBB.class);
intent.putExtra("yourDataKey",yourData)
startActivity(intent);
After that get your value into ActivityAA and load your Fragment with desired data
Intent intent = getIntent();
String yourValue = intent.getExtra("yourDataKey");
I solved this use intent and bundel with this flow :
MainActivity(FragmentA, FragmentB, FragmentC)
This activty(eq : from FragmentB) pass data using intent to ActivityBB
ActivityBB
at onClick ListItem in this activity, I pass data using bundle and call MainActivity (because I want to back to my previous fragment with item value from ActivityBB)
I make a condition from bundle at onCreate method at MainActivity to display currentItem(viewPager)
Actually its work, but I think this is not the proper way. I hope there is a solution with proper way from anyone to solve this.

Open a fragment of an activity from another activity

Hi All I want to open the "Text-To-Speech output" fragment of Settings from my application. I think first I need to open the settings activity and then its fragment. I tried setting the ComponentName but it was unable to locate the activity.
Should I use FragmentManager; I couldn't find anything specific to my needs. Can somebody give me some link which might explain it well.
You are right, First You need to start the Activity than set the current Fragment in FragmentPager / Manager... Their is no such way to start some foreign fragment from your Activity that would be dangerous see that will lead to zombie fragments floating around the App (or May be I am not aware of that..)
You call the Activity Intent with some parameter for the Fragment name, you want to start i.e. interger, boolean etc...
Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra("fragmentNumber",1); //for example
startActivity(intent);
You check the passed value inside OnCreate of the Second Acitivty and set the desired fragment on top.. inside OnCreate
if(getIntent().getIntExtra("fragmentNumber",0)==1){
//set the desired fragment as current fragment to fragment pager
}
However, I am not getting the problem "It was unable to locate the activity." Have you entered the Activity in manifest file than what was the problem you were facing? Please post the full stack trace.
You can use the following:
Intent ttsSettings = new Intent("com.android.settings.TTS_SETTINGS");
ttsSettings.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ttsSettings);

Categories

Resources