Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6789001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:33:07+00:00 2026-05-26T17:33:07+00:00

I am testing fragments in Android and I’m having some confusing behavior with Fragment

  • 0

I am testing fragments in Android and I’m having some confusing behavior with Fragment life cycle. I have a activity that uses layouts in xml for both landscape and portrait modes. I have some code that accesses a EditText object that is defined in one of the fragment layouts. If I start the app in landscape mode things work. I access the component in the onResume() fragment method to update some text.

According to the documentation the onResume() fragment method is called when the fragment is visible to the user. However, if I rotate the screen this method gets called again even though this fragment is not defined in the portrait layout. This call to onCreate causes a NullPointerException becuase this method references the EditText object. Why is onResume method called on a fragment that is not part of the new layout? How and where is the proper place to modify layout data in a fragment. I have tried onStart, onResume, onActivityCreated etc, but all seem to return the same error.

Any help would be most appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:33:07+00:00Added an answer on May 26, 2026 at 5:33 pm

    When you rotate the screen, Android saves your fragments in a Bundle and recreates them when it recreates the activity. That is why you get calls to a non-existing (actually just invisible) fragment. You need to handle this situation in the fragment code, or simply have both fragments in the land and port layouts, where you set the fragment visibility to GONE if you don’t need it.

    A simple way to check if the fragment is visible in code is this:

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (container == null) {
                return null;
            }
      }
    

    If container is null, your fragment is being re-created from a Bundle and won’t be shown (since there is no container). You then have to check whether getView() is null and short-circuit your code accordingly. This can get messy though, so beware 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Being frustrated about fragment behavior, I started doing some testing. I have one activity
for testing purposes i need an recursive directory with some files, that comes to
I have some XML which I am testing, which has set as one of
I've got some OpenGL drawing code that I'm trying to optimize. It's currently testing
I have a fragment class that extends Fragment and calls setHasOptionsMenu to participate in
Testing my app on some WM Std 6.1 I found out that it fails
I have a problem executing android unit tests against android applications that utilize the
I'm looking for some tool that would do basic testing of PHP script. This
I have created a module that works on my Windows XAMPP installation. Testing on
For testing purposes I have to generate a file of a certain size (to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.