For the sake of this question, I've replaced some logic with comments and simplified the layous. But the main purpose of the question should be the same.
So I have an activity view with a button and a textview.
layout_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click me"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Code behind MainActivity.java
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// retrieve data from internet and display in the textview
findViewById(R.id.textView).setText(response);
}
});
Now with this library, I can display the layout_main
with a service in a floating window outside of the activity. See demo gif on the github page. With another button on my xml I start the service.
startService(new Intent(MainActivity.this, MyService.class));
Demo code from the github page:
MyService.java
@Override
public void onCreate() {
super.onCreate();
windowManagerContainer = new WindowManagerContainer(this);
chatHeadManager = new DefaultChatHeadManager<String>(this, windowManagerContainer);
chatHeadManager.setViewAdapter(new ChatHeadViewAdapter<String>() {
@Override
public View attachView(String key, ChatHead chatHead, ViewGroup parent) {
View cachedView = viewCache.get(key);
if (cachedView == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_main, parent, false);
// 'problem' here
cachedView = view;
viewCache.put(key, view);
}
parent.addView(cachedView);
return cachedView;
}
...
But now the problem is, I have to create duplicate code in the service to handle the network requests and display the text. It is basically the same code in the activity.
So my question is, can I somehow share the 'code-behind' to use for my activity and my service or do I have to create a duplicate of the code from my activity?
I can indeed just put my view as a paramater of a class to handle both views there
For my activity:
View view = getLayoutInflater().inflate(R.layout_main, null);
setContentView(view);
new ViewHandler(MainActivity.this, view);
...
For my service:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_main, false);
new ViewHandler(MyService.this, view);
The ViewHandler:
public ViewHandler(Context context, View view) {
super(context, view);
// get ids from the view and attach handlers or do other things with it
Button button = (Button) view.findViewById(R.id.button);
....
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments