SupportFragmentManager replace not working - android

FIXED
I outsourced the Bluetooth scanning to a separate thread which was a failure. :D Having fixed this the code now works. Thanks for all support!
ORIGINAL QUESTION
I'm having troubles with exchanging dynamically added fragments (I use the support library and my minimal API version is 8 (Android 2.2)). In my XML which can be seen below file I have a FrameLayout which contains the fragment.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".BluetoothConnectionManager">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frmlFragmentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/tbBottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#color/hafnertec"
android:layout_alignParentBottom="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/lblMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lblMode"
android:layout_centerInParent="true"/>
<ImageButton android:id="#+id/btnRefresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_refresh"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_alignRight="#id/lblMode"
android:clickable="false"
android:longClickable="false"
android:onClick="updateData"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</RelativeLayout>
For exchanging the fragments i use a SupportFragmentManager. Furthermore, I also instantiate the two fragments in the onCreate() method. In addition, the fragment for discovering Bluetooth devices is added to the frame layout which works fine.
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
// fetch a FragmentManager used for exchanging the fragments
this.fmFragmentExchanger = this.getSupportFragmentManager();
this.btDiscoveryFragment = new BluetoothDiscoveryFragment();
this.btConnectorFragment = new BluetoothConnectorFragment();
if (this.findViewById(R.id.frmlFragmentContainer) != null)
{
this.fmFragmentExchanger.beginTransaction().add(R.id.frmlFragmentContainer, this.btDiscoveryFragment).disallowAddToBackStack().commit();
}
...
}
Moreover, I've taken care that the fragments extend the Fragment class provided by the support library:
import android.support.v4.app.Fragment;
public class BluetoothDiscoveryFragment extends Fragment implements ...
{
// see: https://developer.android.com/training/basics/fragments/creating.html
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Nullable Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_connection_bluetooth_device, container, false);
}
Here is my code for exchanging the fragments:
this.fmFragmentExchanger.beginTransaction().replace(R.id.frmlFragmentContainer, this.btConnectorFragment).disallowAddToBackStack().commit();
The class for discovering Bluetooth devices makes use of a BroadcastReceiver which is "connected" with the activity. On stopping the discovery process or when it has finished I unregister this BroadcastReceiver.
However, on exchanging the fragments nothing happens and after some time I get an error caused by SIGABRT:
12-31 15:09:51.621 9790-9795/com.hafnertec.afdbluetooth I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
12-31 15:09:53.945 9790-9790/com.hafnertec.afdbluetooth A/libc﹕ Fatal signal 6 (SIGABRT) at 0x000001bd (code=0), thread 9790 (ec.afdbluetooth)
Here you can see the file /data/anr/traces.txt: trace file
I'm running the tests on a Samsung Galaxy S I9000 with Android 4.4.4 (CM SNAPSHOT M12). During the activity's onCreate() method I add the btDiscoveryFragment instance which works fine:
this.fmFragmentExchanger.beginTransaction().add(R.id.frmlFragmentContainer, this.btDiscoveryFragment).disallowAddToBackStack().commit();
Furthermore, adding the instance btConnectorFragment using .add() works. However, it logically causes the btConnectorFragment to be overlayed over the btDiscoveryFragment.

I think you have a typo, causing btDiscoveryFragment to be null:
this.fmFragmentExchanger = this.getSupportFragmentManager();
//add this line
this.btDiscoveryFragment = new BluetoothDiscoveryFragment();
//remove this duplicated line
//this.btConnectorFragment = new BluetoothConnectorFragment();
this.btConnectorFragment = new BluetoothConnectorFragment();

Related

Can Android layout be loaded in the C language?

This link says that to make the app display a layout, you create a main_layout.xml file:
<?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="match_parent"
android:orientation="vertical" >
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
and then load it in onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// main_layout.xml is compiled into the R.layout.main_layout object
setContentView(R.layout.main_layout);
}
My problem is that I am coding in C, and have the C version of "onCreate" method:
JNIEXPORT
void ANativeActivity_onCreate(ANativeActivity *activity, void *savedState,
size_t savedStateSize) {
....
}
is it possible to load the layout and make the app display it in the C language?
C uses "ANativeActivity" that it doesn't has any of Java methods and resources/objects. Even if you manually parse that XML you should MANUALLY implement all Widgets/Components in C.....so it's near impossible due to high amount of work involved in it.
Usually someone chooses C on Android to do "some special work" that is not available on normal Java or due to performance issues on it.

Android TextView.setText() is not changing on recent Samsung phones with SurfaceView Overlay

In my app I have a TextView and an ImageView that I update based on sensor information. This works perfectly on emulators and most of the handsets I have tried it on, however on Samsung handsets from the S7 to the S9 for some reason neither of them changes when they should be updated.
They are part of a Fragment overlaying a SurfaceView with a camera preview in it, there is a second SurfaceView overlay above them:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="myApp.CameraViewFragment">
<FrameLayout
android:id="#+id/control"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<myApp.CameraSurfaceView
android:id="#+id/camera_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/direction"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:background="#color/black_overlay"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textSize="30dp"
android:text="" />
<ImageButton
android:id="#+id/gpsNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gps_unfixed"
android:layout_gravity="right"
android:tint="#color/gps_unfixed"
android:background="#null" />
</FrameLayout>
<com.myapp.OverlaySurfaceView
android:id="#+id/overlay_view"
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="center"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</FrameLayout>
As far as I know, this is the relevant part of the Fragment code:
public class CameraViewFragment extends Fragment
{
private TextView directionView;
private CameraSurfaceView cameraView;
private OverlaySurfaceView overlay;
#Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
cameraView = (CameraSurfaceView) view.findViewById(R.id.camera);
directionView = (TextView) view.findViewById(R.id.direction);
overlay = (OverlaySurfaceview) view.findViewById(R.id.overlay_view);
}
private void setDirection(AccurateDirection direction) {
final String directionString = direction.getDirection();
overlay.update();
Activity activity = getActivity();
if ( activity != null ) {
activity.runOnUiThread(new Runnable() {
public void run() {
directionView.setText(directionString);
directionView.invalidate();
}
});
}
}
}
When I run this on a regular handset, the directionView text updates as new direction data arrives. On a Samsung S7 the value gets set once, maybe updated once right at the start and then it seems to stick and not change any more. If I pause in the debugger and call directionView.getText() from the immediate window, it gives me the result I am expecting, not what I see on the screen. If I call directionView.setText('ABC') in the immediate window, the value returned by directionView.getText() changes but again the value on the screen does not. The ImageView also seems to get stuck in its initial state regardless of changes that should adjust it.
Previous questions on this topic seem to involve updates not being triggered from the UI thread or failures to call invalidate on the view component, so I have fixed both of those already but the problem persists.
I have only seen this on recent Samsung handsets ( my old Samsung S4 appears to be fine ) which makes it hard to troubleshoot. Why are my view components not changing on the Samsung devices and what do I need to do to get them to update?
Edit: Apologies to anyone previously attempting to answer this as I had ommited the OverlaySurfaceView and I now realise that the other components update correctly if the OverlaySurfaceView is removed, so apparently having that second SurfaceView in the view hierarchy is interfering, but only on Samsung. Switching on and off hardware acceleration doesn't seem to make a difference, but there may be a specific place or way that I need to do it. On a working handset hardware acceleration is marked as disabled for the `OverlaySurfaceView.
I had the same issue with Samsung S7 device,
eventually I realized i was not handling the setZOrderOnTop correctly
check out this answer :
Button on top of SurfaceView with setZOrderOnTop set to true in Android

Android View.GONE not working as intended on rare occasions

I've been seeing an issue in an app I'm working on relating to views being visible when they shouldn't be and I was wondering if anyone had seen similar.
I have a fragment viewed from a viewPager, that fragment has a base layout, in that base layout I include a banner style area with some info in it and a button, the layout is defaulted to GONE in the XML, like this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/usage_swipe_container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:animateLayoutChanges="true"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/usage_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="#color/background_colour">
<include layout="#layout/layout_in_question"
android:id="#+id/layout_in_question_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
.... Normal Stuff in layout
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
This view is only set to VISIBLE in very select circumstances. However some users are seeing it outside of those circumstances based on a response from a server call, its a very low percentage of users who are seeing it when they shouldn't ( in the region of 0.001% ), the server team insist its not them so I'm trying to ascertain if there are any known android issue or "hacks" that allow this sort of thing. The App supports API 15 and up and we're currently using support libs 25.3.0.
Has anyone seen behavior like this before? are there developer options on some devices that "show all views" or Modded OS's that allow it?
Edit: While I cant share full code snippets due to NDA, I have 2 locations the view can be enabled, both call the same method.
private View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
setupBanner();
}
/**
* OTTO event based on server response
*/
#Subscribe
public void receiveEvent(BannerEvent event)
{
setupBanner();
}
public void setupBanner() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (shouldShowBanner()) {
findViewById(R.id.layout_in_question_id).setVisibility(View.VISIBLE);
}
}
});
}
shouldShowBanner returns a boolean which is either true/false depending on what the server responded with.

Set-and-swipe Forms using Android Fragments

I am trying to develop a very simple Android app for rapid data collection, where an activity displays a fragment containing a form with a number of RadioGroup elements. I would like to have the user be able to quickly tap out the radio selections, then be able to swipe the form to the right to dispose of the fragment, record the data, and bring up another empty form.
What I can't figure out is how to set up the "swipe and replace" feature. I'm thinking of something similar to the ViewPager support class, except no scrolling backwards and an unlimited number of pages. What classes can I use to achieve this kind of behavior? Here's my stripped-down code:
fragment_record.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
<LinearLayout
android:id="#+id/mainLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Group 1"
android:id="#+id/textView" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/Group1">
<!-- Radio Buttons /-->
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Group 2"
android:id="#+id/textView2" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/Group2">
<!-- Radio Buttons /-->
</RadioGroup>
</LinearLayout>
</ScrollView>
RecordFragment.java:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class RecordFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_record, container, false);
return view;
}
}
activity_record.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container" >
</FrameLayout>
RecordActivity.java
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.View;
public class RecordActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) return;
RecordFragment theFragment = new RecordFragment();
theFragment.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(R.id.fragment_container, theFragment).commit();
}
}
}
Ok, the simple answer to this was delivered obiter dicta in the tech talk a month or two ago where Google showed the preview of Android Studio 2.0. One of the presenters said 'we see a lot of people out there using Fragments in places where they should be using ViewGroups.' I was one of those people too.
A wizard is a complex problem: you are usually building something incrementally, so there is state, dependencies, and flow. It's hard to make really generic because what is being built is completely different. Also, it's a clear anti pattern to partially construct the target object leaving a bunch of nullable fields to be filled in as the process grinds on.
If each group can contain a part, then you can assemble the parts and build the target from the parts. There's no shame in it if those parts are not necessarily organic domain objects: if you are modeling the process of obtaining a job, there can be an Application (the thing the user has to fill out) and the CV or Submission (what is built in the form to represent a specific request for consideration).

No View found for id X for Fragment Y

I'm attempting to use fragments to give a different display depending on whether the app is being run on a phone or a tablet - as fragments are supposed to be used.
I have the following XML for my layouts:
layout/main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/list_fragment_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
layout-large/main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false" >
<RelativeLayout
android:id="#+id/list_fragment_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
<RelativeLayout
android:id="#+id/sales_agreement_fragment_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7" />
</LinearLayout>
I havetried using FrameLayouts instead of the RelativeLayouts in the second example - it didn't seem to make any difference.
My Activity onCreate() code is as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager manager = getFragmentManager();
View salesAgreementLayout = findViewById(R.id.sales_agreement_fragment_view);
Fragment formsListFragment = new KingdomSpasFormsListFragment();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.list_fragment_view,formsListFragment);
if (salesAgreementLayout != null) {
Fragment salesAgreementFragment = new SalesAgreementFragment();
fragmentTransaction.add(R.id.sales_agreement_fragment_view, salesAgreementFragment);
}
fragmentTransaction.commit();
}
However, running this (whether on a phone or a tablet results in the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kingdomspas.android.kingdomspasforms/com.kingdomspas.android.kingdomspasforms.activities.KingdomSpasFormsActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f070000 (com.kingdomspas.android.kingdomspasforms:id/list_fragment_view) for fragment KingdomSpasFormsListFragment{527f671c #0 id=0x7f070000}
I've Googled this extensively as it seems to be a common problem. The main solution, however, seem to be that the original layout specified in setContentView() doesn't contain the Views later referenced in the add() methods. However, this is definitely not the problem in my case (unless I'm being ridiculously blind - I've checked and re-checked).
In fairness, I'm not actually certain that this is the best UI design for my app - I'm wondering about tabs perhaps - but, either way, I'd really like to understand what the problem is here and what I'm doing wrong - else I'm likely doomed to repeat it in the future.
Here is a great tutorial on multipane fragments for android. http://www.vogella.com/tutorials/AndroidFragments/article.html
With regards to your problem if you add static fragments in your layout it will solve your problem. If you have a definite need for dynamic fragments I would suggest doing a check for what sort of device that application is running on before you find the view i.e.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager manager = getFragmentManager();
View salesAgreementLayout = findViewById(R.id.sales_agreement_fragment_view);
Fragment formsListFragment = new KingdomSpasFormsListFragment();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.list_fragment_view,formsListFragment);
if (isTablet()){
Fragment salesAgreementFragment = new SalesAgreementFragment();
fragmentTransaction.add(R.id.sales_agreement_fragment_view, salesAgreementFragment);
}
fragmentTransaction.commit();
}
public boolean isTablet() {
return (getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
the is tablet method was adapted from How to detect device is Android phone or Android tablet?

Categories

Resources