I have three activities in my app
A login activity
A main activity
A detail activity
I want to use espresso to test a sequence of events: click the login button on the login activity, which opens the main activity, and then click a list item in main activity, which opens detail activity, and then click another button in the detail activity. I started by creating this simple test, to get a reference to the listview:
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {
public LoginActivityTest() {
super(LoginActivity.class);
}
#Override
public void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testSequence() throws Exception {
// Login
onView(withId(R.id.button_log_in)).perform(click());
// Check if MainActivity is loaded
onView(withId(R.id.container)).check(matches(isDisplayed()));
// Check if Fragment is loaded
onView(withId(R.id.list)).check(matches(isDisplayed()));
}
}
On the mainActivity onCreate() method I load a fragment like this:
getFragmentManager().beginTransaction()
.add(R.id.container, mListFragment)
.commit();
The ListFragment fragment has a list (R.id.list), but still the test fails with a NoMatchingViewException:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.tests.android.development:id/list
What am I doing wrong?
A note from the documentation for onView:
Note: the view has to be part of the view hierarchy. This may not be
the case if it is rendered as part of an AdapterView (e.g. ListView).
If this is the case, use Espresso.onData to load the view first.
To use onData to load the view, you need to check for instances of whatever your adapter is in the ListView. In other words, if your listview uses a Cursor adapter, you can try this:
onData(allOf(is(instanceOf(Cursor.class)))).check(matches(isDisplayed()));
It is important to note that the above will only pass if your listview contains at least one item. It is a good idea to have one test where an item exists, and one test where an item does not.
For more information on how to check for data that does exist, see here.
For more information on how to check for data that does not exist in an adapter, see here.
In the current version (Espresso 2.2.2) this exception is always appended with a View Hierarchy: clause that lists all the views available to match. Stroll through that and check if you can find your list.
As an alternative: check out android-sdk\tools\uiautomatorviewer.bat (or .sh) which takes a snapshot from the current screen and hierarchy. Put a breakpoint on your list matching line and check with the viewer if the list is there. If you find the list, there may be a timing issue in the test. Maybe it didn't wait enough, check out more about IdlingResources.
Related
Good evening,
I am new at Android App development. I have a project where I receive data from multiple sensors which are connected an instrument.
At this time, I can select any sensor and stream its data using AudioTrack. The streaming is from a service and this service is bound to the MainActivity instance. A single sensor data is streamed at the time. This is working as expected.
Now, I want to plot (using MPAndroidChart) the data for each sensor in its own chart. In the activity_main layout, I added a ViewPager2 element, and Fragments are instantiated by the PagerAdapter::CreateFragment(int position) method.
My class model is as follow (based on a ViewPager2 example):
public class MainActivity extends AppCompatActivity
public class PagerAdapter extends FragmentStateAdapter
public class ChannelFragment extends Fragment
public class SensorViewModel extends ViewModel
I tested the visualisation of the plots in the Fragments using mock data (and a different colour background). The mock data was generated in ChannelFragment::onCreateView() method. This also works, though the mock data is the same for all fragments/charts, but different background colour for each fragment.
In a live test, I also proved to myself that I process changing the visible fragment correctly. I associate each fragment to a specific sensor and the streaming data changes as the visible fragment changes. This is done from MainActivity using ViewPager2.OnPageChangeCallback onPageSelected(position)
I am able to set the sensor data to the SensorViewModel and trigger a notification when calling postValue() from SensorViewModel.
The SensorViewModel contains the following declaration
private MutableLiveData<float[][]> sensorLiveData = new MutableLiveData<>();
and is created in MainActivity::onCreate(Bundle savedInstanceState)
// Create a ViewModel to hold the audio (sensor) data. This view model is used to communicate
// with the UI, graphically display the audio data for example
sensorViewModel = new ViewModelProvider(this).get(SensorViewModel.class);
The code for the observer is defined as a ChannelFragment method and listed as
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
sensorViewModel = new ViewModelProvider(requireActivity()).get(SensorViewModel.class);
sensorViewModel.getSampleData().observe(requireActivity(), new Observer<float[][]>() {
#Override
public void onChanged(#Nullable float[][] sampleData)
{
// Update the sample data graphical representation
Log.d(TAG, String.format("ChannelFragment; onChanged() sampleData length : %d, size: %d",
sampleData.length, sampleData[0].length));
addEntry(sampleData);
}
});
And the log statement reports
ChannelFragment: ChannelFragment; onChanged() sampleData length : 8, size: 170
I am stuck as to update the correct chart (correct ChannelFragment) with its corresponding sensor data.
How to a plot sampleData[0][] in Fragment at position 0, sampleData1 in Fragment at position 1, and so forth?
Calls to ChannelFragment.getId() always returns 0, though I am able to access other attributes such as title when different fragments become visible.
I also modified the SensorVIewModel as suggested by Tiago Redaelli. The new declaration:
private HashMap<Integer, MutableLiveData<ArrayList<Float>>> sensorLiveData = new HashMap<>();
where the key identifies the sensor/fragment the MutableLiveData belongs to. However, the example does not explain how to set (inject?) the ViewModel (and MutableLiveData value) to the ChannelFragment. In the example, the method setViewModel() is implemented but call is not shown.
Using this new ViewModel definition, how do I associate each HashMap values with their corresponding Fragment (who is calling setViewModel() ? What I am missing?
Thanks in advance
Daniel
Just a quick update. I was trying to create a complicated solution to my issue, including looking at ViewModel factory for example.
In my solution, I simply call setViewModel() (mentioned above) using the position/index value passed as argument when creating an instance of ChannelFragment.
This appears to be working.
This question is mostly to solicit opinions on the best way to handle my app. I have three fragments being handled by one activity. Fragment A has one clickable element the photo and Fragment B has 4 clickable elements the buttons. The other fragment just displays details when the photo is clicked. I am using ActionBarSherlock.
The forward and back buttons need to change the photo to the next or previous poses, respectively. I could keep the photo and the buttons in the same fragment, but wanted to keep them separate in case I wanted to rearrange them in a tablet.
I need some advice - should I combine Fragments A and B? If not, I will need to figure out how to implement an interface for 3 clickable items.
I considered using Roboguice, but I am already extending using SherlockFragmentActivity so that's a no go. I saw mention of Otto, but I didn't see good tutorials on how to include in a project. What do you think best design practice should be?
I also need help figuring out how to communicate between a fragment and an activity. I'd like to keep some data "global" in the application, like the pose id. Is there some example code I can see besides the stock android developer's information? That is not all that helpful.
BTW, I'm already storing all the information about each pose in a SQLite database. That's the easy part.
The easiest way to communicate between your activity and fragments is using interfaces. The idea is basically to define an interface inside a given fragment A and let the activity implement that interface.
Once it has implemented that interface, you could do anything you want in the method it overrides.
The other important part of the interface is that you have to call the abstract method from your fragment and remember to cast it to your activity. It should catch a ClassCastException if not done correctly.
There is a good tutorial on Simple Developer Blog on how to do exactly this kind of thing.
I hope this was helpful to you!
The suggested method for communicating between fragments is to use callbacks\listeners that are managed by your main Activity.
I think the code on this page is pretty clear:
http://developer.android.com/training/basics/fragments/communicating.html
You can also reference the IO 2012 Schedule app, which is designed to be a de-facto reference app. It can be found here:
http://code.google.com/p/iosched/
Also, here is a SO question with good info:
How to pass data between fragments
It is implemented by a Callback interface:
First of all, we have to make an interface:
public interface UpdateFrag {
void updatefrag();
}
In the Activity do the following code:
UpdateFrag updatfrag ;
public void updateApi(UpdateFrag listener) {
updatfrag = listener;
}
from the event from where the callback has to fire in the Activity:
updatfrag.updatefrag();
In the Fragment implement the interface in CreateView do the
following code:
((Home)getActivity()).updateApi(new UpdateFrag() {
#Override
public void updatefrag() {
.....your stuff......
}
});
To communicate between an Activity and Fragments, there are several options, but after lots of reading and many experiences, I found out that it could be resumed this way:
Activity wants to communicate with child Fragment => Simply write public methods in your Fragment class, and let the Activity call them
Fragment wants to communicate with the parent Activity => This requires a bit more of work, as the official Android link https://developer.android.com/training/basics/fragments/communicating suggests, it would be a great idea to define an interface that will be implemented by the Activity, and which will establish a contract for any Activity that wants to communicate with that Fragment. For example, if you have FragmentA, which wants to communicate with any activity that includes it, then define the FragmentAInterface which will define what method can the FragmentA call for the activities that decide to use it.
A Fragment wants to communicate with other Fragment => This is the case where you get the most 'complicated' situation. Since you could potentially need to pass data from FragmentA to FragmentB and viceversa, that could lead us to defining 2 interfaces, FragmentAInterface which will be implemented by FragmentB and FragmentAInterface which will be implemented by FragmentA. That will start making things messy. And imagine if you have a few more Fragments on place, and even the parent activity wants to communicate with them. Well, this case is a perfect moment to establish a shared ViewModel for the activity and it's fragments. More info here https://developer.android.com/topic/libraries/architecture/viewmodel . Basically, you need to define a SharedViewModel class, that has all the data you want to share between the activity and the fragments that will be in need of communicating data among them.
The ViewModel case, makes things pretty simpler at the end, since you don't have to add extra logic that makes things dirty in the code and messy. Plus it will allow you to separate the gathering (through calls to an SQLite Database or an API) of data from the Controller (activities and fragments).
I made a annotation library that can do the cast for you. check this out.
https://github.com/zeroarst/callbackfragment/
#CallbackFragment
public class MyFragment extends Fragment {
#Callback
interface FragmentCallback {
void onClickButton(MyFragment fragment);
}
private FragmentCallback mCallback;
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1
mCallback.onClickButton(this);
break;
case R.id.bt2
// Because we give mandatory = false so this might be null if not implemented by the host.
if (mCallbackNotForce != null)
mCallbackNotForce.onClickButton(this);
break;
}
}
}
It then generates a subclass of your fragment. And just add it to FragmentManager.
public class MainActivity extends AppCompatActivity implements MyFragment.FragmentCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.add(R.id.lo_fragm_container, MyFragmentCallbackable.create(), "MY_FRAGM")
.commit();
}
Toast mToast;
#Override
public void onClickButton(MyFragment fragment) {
if (mToast != null)
mToast.cancel();
mToast = Toast.makeText(this, "Callback from " + fragment.getTag(), Toast.LENGTH_SHORT);
mToast.show();
}
}
Google Recommended Method
If you take a look at this page you can see that Google suggests you use the ViewModel to share data between Fragment and Activity.
Add this dependency:
implementation "androidx.activity:activity-ktx:$activity_version"
First, define the ViewModel you are going to use to pass data.
class ItemViewModel : ViewModel() {
private val mutableSelectedItem = MutableLiveData<Item>()
val selectedItem: LiveData<Item> get() = mutableSelectedItem
fun selectItem(item: Item) {
mutableSelectedItem.value = item
}
}
Second, instantiate the ViewModel inside the Activity.
class MainActivity : AppCompatActivity() {
// Using the viewModels() Kotlin property delegate from the activity-ktx
// artifact to retrieve the ViewModel in the activity scope
private val viewModel: ItemViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.selectedItem.observe(this, Observer { item ->
// Perform an action with the latest item data
})
}
}
Third, instantiate the ViewModel inside the Fragment.
class ListFragment : Fragment() {
// Using the activityViewModels() Kotlin property delegate from the
// fragment-ktx artifact to retrieve the ViewModel in the activity scope
private val viewModel: ItemViewModel by activityViewModels()
// Called when the item is clicked
fun onItemClicked(item: Item) {
// Set a new item
viewModel.selectItem(item)
}
}
You can now edit this code creating new observers or settings methods.
There are severals ways to communicate between activities, fragments, services etc. The obvious one is to communicate using interfaces. However, it is not a productive way to communicate. You have to implement the listeners etc.
My suggestion is to use an event bus. Event bus is a publish/subscribe pattern implementation.
You can subscribe to events in your activity and then you can post that events in your fragments etc.
Here on my blog post you can find more detail about this pattern and also an example project to show the usage.
I'm not sure I really understood what you want to do, but the suggested way to communicate between fragments is to use callbacks with the Activity, never directly between fragments. See here http://developer.android.com/training/basics/fragments/communicating.html
You can create declare a public interface with a function declaration in the fragment and implement the interface in the activity. Then you can call the function from the fragment.
I am using Intents to communicate actions back to the main activity. The main activity is listening to these by overriding onNewIntent(Intent intent). The main activity translates these actions to the corresponding fragments for example.
So you can do something like this:
public class MainActivity extends Activity {
public static final String INTENT_ACTION_SHOW_FOO = "show_foo";
public static final String INTENT_ACTION_SHOW_BAR = "show_bar";
#Override
protected void onNewIntent(Intent intent) {
routeIntent(intent);
}
private void routeIntent(Intent intent) {
String action = intent.getAction();
if (action != null) {
switch (action) {
case INTENT_ACTION_SHOW_FOO:
// for example show the corresponding fragment
loadFragment(FooFragment);
break;
case INTENT_ACTION_SHOW_BAR:
loadFragment(BarFragment);
break;
}
}
}
Then inside any fragment to show the foo fragment:
Intent intent = new Intent(context, MainActivity.class);
intent.setAction(INTENT_ACTION_SHOW_FOO);
// Prevent activity to be re-instantiated if it is already running.
// Instead, the onNewEvent() is triggered
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
getContext().startActivity(intent);
There is the latest techniques to communicate fragment to activity without any interface follow the steps
Step 1- Add the dependency in gradle
implementation 'androidx.fragment:fragment:1.3.0-rc01'
Introduction
Huh, this is a tough one. At least, I think so...
To clear things up:
I did not find any answers to my question after searching the internet (using Google).
Everything I found was about people setting up the onClickListener's for their View's wrong. I guess it is the same problem in my case, but none of the other problems matched mine.
This seems to be the same problem... It has no answers.
Setup
I've got three Fragment's set up together with a ViewPager in my AppCompatActivity.
viewPager = (ViewPager) findViewById(R.id.main_view_pager);
viewPager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager()));
Handled by the SectionsPagerAdapter.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return MainTextHolder.newInstance(tabIndicatorOnClick);
case 1:
return PostWriter.newInstance(tabIndicatorOnClick);
case 2:
return TopPage.newInstance(tabIndicatorOnClick);
default:
return Error.newInstance();
}
}
#Override
public int getCount() {
return 3;
}
// ... more methods
}
In each of the Fragment's I have some content plus a custom TabIndicator.
(the following xml file is my View's for the indicator)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:id="#+id/fragment_divider_one"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:tag="one" />
<View
android:id="#+id/fragment_divider_two"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:tag="two" />
<View
android:id="#+id/fragment_divider_three"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:tag="three" />
</LinearLayout>
And then I set an OnClickListener for each of those View's (dividers) in the Fragment's onCreateView(LayoutInflater inflater, ViewGroup Bundle savedInstanceState) method. The OnClickListener is already prepared in my Activity where I also instantiate my ViewPager so that I can change the current Item (Fragment/tab).
private final View.OnClickListener tabIndicatorOnClick = new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getTag().toString().equals("one"))
viewPager.setCurrentItem(0, true); // second argument for smooth transition
else if (view.getTag().toString().equals("two"))
viewPager.setCurrentItem(1, true);
else if (view.getTag().toString().equals("three"))
viewPager.setCurrentItem(2, true);
}
};
I pass that OnClickListener to the Fragment's by putting it into my newInstance(View.OnClickListener tabIndicatorOnClick) method.
This is for one of the fragments. It is identical for the others!
public static MainTextHolder newInstance(View.OnClickListener tabIndicatorOnClick) {
MainTextHolder fragment = new MainTextHolder();
Bundle args = new Bundle();
fragment.putIndicatorTabIndicatorOnClick(tabIndicatorOnClick);
fragment.setArguments(args);
fragment.setRetainInstance(true);
return fragment;
}
My putIndicatorTabIndicatorOnClick(View.OnClickListener tabIndicatorOnClick) method is a Void in an Interface. It just applies the OnClickListener to the Class(Fragment).
#Override
public void putIndicatorTabIndicatorOnClick(View.OnClickListener tabIndicatorOnClick) {
this.tabIndicatorOnClick = tabIndicatorOnClick;
}
Does it work
Yes it does. It works perfectly fine... until a ConfigurationChange happens. In my case I tested it with changing the orientation.
The Problem
What happens after that ConfigurationChange is that everything goes normally. The OnClickListener gets applied to all View's in all Fragment's, but the tabIndicatorOnClick OnClickListener is a null object reference in the second and third Fragment.
So in PostWriter and TopPage there isn't even really an OnClickListener on the View's for what ever reason. But it gets better: In MainTextHolder Fragment the tabIndicatorOnClick is not a null object reference, but it does not change the ViewPager's Item anymore. It runs the code, but it does not scroll the Tab.
When turning on "Don't keep activities" ind Developer options of the device and leaving the app, all OnClickListener's are null object references. Even those in MainTextHolder.
Summary
After the ConfigurationChange the OnClickListener gets passed into my first Fragment, but it is no working properly and in the remaining Fragment's it is not even passed / a null object reference.
The issue can be replicated through destroying the Activity and then reloading it.
In the end...
... I have no clue what is going wrong. I hope that I structured my question properly.
May be of interest
minSdkVersion: 14
targetSdkVersion: 25
I already have a SpringIndicator on my ViewPager. This does not feature an OnClickListener so I added my own "overlayed" indicators which have the needed OnClick feature. I also like the look of it, but if you can give me a better solution for my TabIndicator, I would also love to have that as an answer.
I came across the solution to put the method into the xml :onClick attribute multiple times, but for that I would need to create the same OnClickListener for every Fragment which is not really the nice way and it is also not solving my problem, but a way around it. - I would need to pass the ViewPager into my Fragment and then call that method from xml which is, as I said, just another way of doing it and not the way I prefer to do it. Also I don't know if it works. I'll most likely test it.
Other OnClickListener's in the Fragment still work properly. So as I said the problem lays in the OnClickListener itself not working right/being a null object reference.
Firstly, well done for writing a detailed description of the problem. Many questioners on StackOverflow can learn from the ability of this question to articulate the problem.
This error would seem to come from not accounting for Fragment lifecycle changes. While inside a ViewPager, Fragments will pass through various states including hidden and shown when they are temporarily offscreen, and paused and resumed if they are offscreen and the system frees memory, and finally created and destroyed if the Android system decides to save the instance state of your Activity (such as from a configuration change). In the latter case, Android will try and restore the state of your Fragments from saved instance state in your ViewPager. Since your Fragments have no way to save the View.OnClickedListener they were passed in the arguments, they end up with a null pointer when they are restored which is causing your error.
To fix the error, I suggest that you do not pass the View.OnClickedListener as a parameter to the Fragments. Rather, expose the OnClickListener to your Fragments via a public method in your Activity and have the Fragments get it themselves in their onResume(). This way you can guarantee that the Fragments will have a reference to the View.OnClickedListener whenever they are in a resumed state. So your onResume() might look something like this:
#Override
public void onResume() {
OnClickListener onClickListener = ((MyActivity) getActivity).getOnClickListener();
putIndicatorTabIndicatorOnClick(onClickListener);
}
Since you have set it in onResume(), you should remove it in onPause():
#Override
public void onPause() {
//set the onClickListener to null to free up the resource
}
Using onResume() and onPause() like this to free up resources from listeners is the approach recommended in the developer guide.
Of course, you will have to make sure your Activity also handles its own lifecycle by checking for saved state in onCreate(Bundle savedInstanceState) and restoring it appropriately.
Please note that saving instance state and restoring can be triggered in a number of ways:
Configuration state change (such as rotating the phone and causing the Activity to display in landscape rather than portrait
The device becoming low on memory (for example, if a large number of Activities are in recent apps)
Turning on Developer Options / Don't keep activities and using home to put your application in recent apps and then navigating back to your app.
You will be able to use one of these methods to determine if you have correctly handled the lifecycles of your Activities and Fragments.
I am going through Head First Android Development and I am a bit confused with
this method --> findViewById(int id)
I have the below button in the file "activity_find_beer.xml" :
<Button
android:id="#+id/find_beer"
android:text="#string/find_beer"
android:onClick="onClickFindBeer" />
and the following code from the class FindBeerActivity.java which is taking the user selected beer and displaying the same in a textview.
public class FindBeerActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_beer);
}
//Call when the button gets clicked
public void onClickFindBeer(View view) {
//Get a reference to the TextView
TextView brands = (TextView) findViewById(R.id.brands);
//Get a reference to the Spinner
Spinner color = (Spinner) findViewById(R.id.color);
//Get the selected item in the Spinner
String beerType = String.valueOf(color.getSelectedItem());
//Display the selected item
brands.setText(beerType);
}
}
My Question is the method onClickFindBeer(View view) takes a View type of
object as a parameter , but in the xml i have just mentioned
android:onClick="onClickFindBeer" and when the user clicks the
button , the method onClickFindBeer gets invoked...Who is passing the object of
type View to the onClickFindBeer(View view) ...is it something
implicit ?
Second,on developer.android.com I see that the method
findViewById(int id) is both in the Activity class (
https://developer.android.com/reference/android/app/Activity.html
) and also in the View class
https://developer.android.com/reference/android/view/View.html
... It's not clear to me which class (Activity or View)
findViewById(int id) method is invoked when i call findViewById()
from onClickFindBeer(View view){}.
Would be highly obliged if someone could throw light on this.
Regards.
The method takes a View parameter as that is how it is implemented in a superclass of the Button class (It is public class Button extends TextView.). The views you add to XML are actually java classes. When you set a property to such an XML item, that constructs the object from the particular java class accordingly. The onClick method of the View class goes as onClick(View v). By setting an XML you just asked the Button class to look for the entered method but its signature is always with a View as a paramenter, which refers to the view clicked.
findViewById has to be called on a View group. But the Actyvity class implements it to search an item in view assigned to it by the setContentView() method.
It is done somewhat implicitly. When building your app, the XML file is actually converted into Java file. When you click the view, the view is passed into the onClickFindBeer(View view) function.
The findViewById() is being called here by the activity. You can see the method declaration by clicking on findViewByID while pressing Ctrl. For a view, you would have to call it using the view. For example,
view.findViewById();
Its called JAVA Reflection which is used by android
2.
As I know, main difference is that when you used OnClickListener from activity it is connected with partivular object such as Textview,Button
find_beer.setOnClickListener and below code is excuted when someButton is pressed.
While android:onClick = "onClickFindBeer" is used handle click directly in the view's activity without need to implement any interface
You have assigned the method onClickBeer to your button. When the button gets clicked, the object, in this case the button, is passed to the method you assigned to it. A Button is a type of View object, so you have a more generic View object as the parameter, but you are perfectly ok to caste it to a button object.
findViewById is called through a "context", which is a way of getting at system resources. You are asking the system to return to you a specific object, which you can then use. It is worth reading up on contexts.
Hope that answers some of your query.
Base on your sample above the android:onClick method is the one being invoked because when invoking a onclick method in java class, it need to call a onClickListener.
cause on the other question. as far as I know it belong to the view class because it always to reference an object on your design.
As a little example: Take a website where you land on a login page and after a successful login getting directed to the main page where on the left side you'd have a menu and on the right side a content are where everything gets loaded when clicking a link in the menu. In the menu I'd have multiple stores where each store would have its own settings. Those settings would be initialized with an id to determine which content the view should display.
For the history mapper I guess one would have something like this:
/#LoginPlace:login
/#MainPlace:home
/#MainPlace:storeSettings?id=<id>
/#MainPlace:userSettings
etc..
In [2] it says
"In order to be accessible via a URL, an Activity needs a corresponding Place."
Which sounds to me like I should have a LoginActivity and a MainActiviy since after all LoginActivity is the first Place I arrive when I come to my website and MainActivity is the place I go when I am successfully logged in.
I'd have someting like this:
public class LoginActivity extends AbstractActivity implements LoginView.Presenter {
// ..
private void onLoginSuccess() {
this.clientFactory.getPlaceController().goTo(new MainPlace());
}
}
public class MainActivity extends AbstractActivity implements MainView.Presenter {
// ..
}
and of course have respective view LoginView and MainView.
This is how AppActivityMapper.getActivity() would look like:
#Override
public Activity getActivity(Place place) {
if (place instanceof LoginPlace) {
return new LoginActivity((LoginPlace) place, clientFactory);
} else if (place instanceof MainPlace) {
return new MainActivity((MainPlace) place, clientFactory);
}
return null;
}
So far so good but how would I implement the MenuView and the MainContentView?
I want to be able to click on menu items in the MenuView and update MainContentView and generate place tokens accordingly like:
/#MainPlace:home
/#MainPlace:storeSettings?id=<id>
/#MainPlace:userSettings
But I have no idea how to do that and how I would for example initialize my activity StoreSettingsActivity with the given id. I believe that I would need another MainActivityMapper extends ActivityMapper that now should control the place changes initiated by clicking on menu links but I just can't figure out how that could work.
I hope my question is clear, please let me know if you need some more information.
I think I once did something similar, it was not perfect because I had to deal with an existing architecture but it worked.
Here is what I did:
When a link from the menu is click execute placeController.goTo(new MainPlace(id));
In your ActivityMapper, return your MainActivity in which you set the id of the MainPlace.
In your MainActivity, in the start method call a method that will initiate your MainContentView depending on the id of MainPlace you set earlier in the MainActivity.
I am pretty sure there is a better way of doing this, but it works and it can be a start that might help you to find a better solution.