2

I am new to Android development and have added an additional item to a Navigation menu which successfully opens an external browser window but clears the app screen (except for the navigation menu) when startActivity in onActivityCreated is performed.

enter image description here

I am not making any more progress despite working on this for several days so I am asking for help now. Perhaps this is a simple question for someone with more experience. I have spent a week looking at all the other answers in SO, YouTube and Google Developer Courses trying to solve this so I am asking for help now.

WebsiteFragment.java

public class WebsiteFragment extends Fragment {
    View myFragment;

    RecyclerView websiteList;
    LinearLayoutManager layoutManager;

    private static final String COMMON_TAG="CombinedLifeCycle";
    private static final String ACTIVITY_NAME =WebsiteFragment.class.getSimpleName();
    private static final String TAG=ACTIVITY_NAME;

    public static WebsiteFragment newInstance() {
        WebsiteFragment websiteFragment = new WebsiteFragment();
        return websiteFragment;    
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable     ViewGroup container, @Nullable Bundle savedInstanceState) {
        //Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_website, container, false);

        Log.i(TAG,ACTIVITY_NAME+" onCreateView");

        websiteList = (RecyclerView)view.findViewById(R.id.websiteList);
        layoutManager = new LinearLayoutManager(getActivity());
        websiteList.setHasFixedSize(true);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse("https://english-alps.com"));
        // App Window is cleared??
        getActivity().startActivity(i);
        Log.i(TAG,ACTIVITY_NAME+" onActivityCreated");    
    }
}

fragment_webiste.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".WebsiteFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/websiteList"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </androidx.recyclerview.widget.RecyclerView>

</FrameLayout>

Edit 21-08-2018

Added the following code to try and save Fragment state but it generates the error:

java.lang.NullPointerException: Attempt to read from field 'int androidx.fragment.app.Fragment.mIndex' on a null object referen.

I am having trouble with the parameters to getFragmentManager. How should the variables to getFragmentManager be initialized?

public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState!=null){
        Log.i(TAG,ACTIVITY_NAME+" calling onSaveInstanceState");
        onSaveInstanceState(savedInstanceState);
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    getFragmentManager().putFragment(outState,TAG,myFragment);
    Log.i(TAG,ACTIVITY_NAME+" onSaveInstanceState");
}

Edit#2 21-08-2018

Home.Java which adds WebsiteFragment to the Activity

public class Home extends AppCompatActivity {

    BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                Fragment selectedFragment = null;
                switch (menuItem.getItemId()) {
                    case R.id.action_category:
                        selectedFragment = CategoryFragment.newInstance();
                        break;
                    case R.id.action_ranking:
                        selectedFragment = RankingFragment.newInstance();
                        break;
                    case R.id.action_website:
                        selectedFragment = WebsiteFragment.newInstance();
                        break;
                }
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.frame_layout, selectedFragment);
                transaction.addToBackStack(String.valueOf(R.id.frame_layout)); /* 21.08.2018 */
                transaction.commit();
                return true;
            }

        });
        setDefaultKeyMode();

    }
}

Edit #3 21-08-2018 activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".Home">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:animateLayoutChanges="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigation"></FrameLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        app:itemIconTint="@drawable/nav_item_color_state"
        app:itemTextColor="@drawable/nav_item_color_state"
        app:menu="@menu/bottom_menu"
    />
</RelativeLayout>  
samus
  • 6,102
  • 6
  • 31
  • 69
Nexus7_2012
  • 654
  • 9
  • 13
  • Remember onActivityCreated() call first before onCreateView() – Rahul Chokshi Aug 21 '18 at 01:10
  • refer this link https://www.androidhive.info/2016/01/android-working-with-recycler-view/ –  Aug 21 '18 at 06:58
  • I am still spinning my wheels. I tried to implement: public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); getFragmentManager().putFragment(outState,TAG,outState); Log.i(TAG,ACTIVITY_NAME+" onSaveInstanceState"); } – Nexus7_2012 Aug 21 '18 at 12:27
  • How is the `WebsiteFragment` added to the Activity? Are you trying to build a cache-list of visited websites in the RecyclerView? Or, are you maybe trying to embed a browser into the main activity? – samus Aug 21 '18 at 15:24
  • `onSaveInstanceState` shouldn't be called from `onCreate`, it's an override called from runtime. Fragments have special handling of the InstanceState https://stackoverflow.com/questions/15313598/once-for-all-how-to-correctly-save-instance-state-of-fragments-in-back-stack – samus Aug 21 '18 at 15:49
  • @samara: The question has been edited to include the Home.java file where the WebsiteFragment is added to the Activity. The browser call is working and opening a web browser, BUT the app screen is going BLANK except for the navigation menu buttons. There is a transaction.addToBackStack call in Home.java but the app screen is still blanked upon the getActivity().startactivity(i) call in the onActivityCreated method which is opening the website. The goal is to prevent the app screen from going blank. – Nexus7_2012 Aug 21 '18 at 15:59
  • Ok, thanks for clarifying. Can you also post the layout xml of the main activity. – samus Aug 21 '18 at 16:09
  • It runs ok using the support library RecyclerView, but I cannot not test the androidx library right now b/c I don't want to corrupt my production environment on the Xamarin/VS side by installing API 28. I recently set them up to both point to the same Android SDK, but I need to separate them again. – samus Aug 21 '18 at 18:29
  • activity_home.xml has been posted into the question. Just to clarify: The Website Navigation action should only open the browser to the predefined URL and it is doing that. I am simply trying to keep the app screen from going blank when it does this. I am a beginner and this is my first app. – Nexus7_2012 Aug 21 '18 at 18:34
  • I'm installing API 28 and will give the androidx library a try to see if I can reproduce the issue. Are you deploying to an emulator or hardware? – samus Aug 21 '18 at 19:06
  • The issue appears on both Emulators in Android Studio running API 26 and API 23. Phone is API 23 (Marshmallow 6.0) and issue appears on phone also. – Nexus7_2012 Aug 21 '18 at 19:46
  • Can you please post your *Gradle Scripts - build.gradle(Module: app)* file. – samus Aug 21 '18 at 19:51
  • ... and also the `Adapter` implementation for the `RecyclerView` ... – samus Aug 21 '18 at 19:52
  • You can download them from github for the next hour or so. I changed the permissions and the repository is public now for the next hour or so. https://github.com/wtb2015/EnglishQuiz/ – Nexus7_2012 Aug 21 '18 at 20:03
  • Ok... In **activity_home.xml**, try setting the `FrameLayout`'s `layout_width` and `layout_height` to *wrap_content* instead of *match_parent*. – samus Aug 21 '18 at 20:09
  • This setting caused the screen to go blank also: – Nexus7_2012 Aug 21 '18 at 20:19
  • There is no main activity source file in github project. I both cloned and downloaded zip. It builds, but fails when run - *Unable to instantiate activity ComponentInfo{com.english_alps.myapplication/com.english_alps.myapplication.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.english_alps.myapplication.MainActivity"* – samus Aug 21 '18 at 20:49
  • added MainActivity.java now....sorry....I am new to github also and it appears the android studio windows interface to github is cumbersome – Nexus7_2012 Aug 21 '18 at 21:50
  • I no longer have access. – samus Aug 22 '18 at 11:54
  • I can add your user name to the github repository, that is better than making the repository public because that exposes the google API to the world. What is your github name ? – Nexus7_2012 Aug 22 '18 at 20:01

0 Answers0