0

When i go from MainActivity to EditTaskActivity (a screen where you can edit task options), rotate the screen and then try to save the task, the app crashes.

Now i found out that it crashes inside MainActivity's onActivityResult, when trying to save the task's data into sectionsPagerAdapter.tasks, because "tasks" is null.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == RequestType.REQUEST_ADD_ITEM.ordinal) {
        if (resultCode == Activity.RESULT_OK) {
                //This is where it crashes
                sectionsPagerAdapter.tasks.addItem(Task(data?.getStringExtra("name") as String, 
                    data.getLongExtra("dueDate", 0)
                ))
            }
        }...

SectionsPagerAdapter (a FragmentPagerAdapter) is created inside MainActivity onCreate, so that is fine.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager)
    val viewPager: ViewPager = findViewById(R.id.view_pager)
    viewPager.adapter = sectionsPagerAdapter
    val tabs: TabLayout = findViewById(R.id.tabs)
    tabs.setupWithViewPager(viewPager)

The problem is that tasks are created inside the instantiateItem function, which for some reason gets called after the onActivityResult. Is there anything i can do, so that instantiateItem gets called before the onActivityResult?

class SectionsPagerAdapter(private val context: Context, fm: FragmentManager)
: FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
lateinit var tasks: ListFragment

override fun getItem(position: Int): Fragment {
    return ListFragment.newInstance(position)
}

override fun instantiateItem(container: ViewGroup, position: Int): Any {
    val fragment: ListFragment = super.instantiateItem(container, position) as ListFragment

    tasks = fragment

    return fragment
}

I feel like i've tried everything - calling getItem(), changing to FragmentStatePagerAdapter and saving it's state, but nothing works and the app still crashes. I'm clueless on this one, so any help will be greatly appreciated :)

Tomas Loksa
  • 95
  • 1
  • 10
  • If i didn't do a good job explaining, here's a link to the github repo: [link](https://github.com/tomasloksa/HabitDo/tree/main/app/src/main/java/com/example/habitdo) - the relevant folders are "adapter" and "ui/main". – Tomas Loksa May 18 '21 at 22:04
  • When the orientation changes, you may want to stop recreating your activity so "tasks" stays the same, try [this](https://stackoverflow.com/questions/10530760/android-stop-recreating-the-activity-on-orientation-change) – javdromero May 18 '21 at 22:20
  • @javdromero if your app isn't ready for orientation changes that means it also don't handle lifecycle events properly (OS may kill your activity) – Romadro May 18 '21 at 22:41
  • 1
    @Romadro I was at first trying to avoid doing something like this, but it's for a graded assignment, which has a requirement of "responding correctly to rotation changes". And this somehow does the job of meeting the criteria. One day, if i decide to publish the app i'll sure fix it before that :) – Tomas Loksa May 19 '21 at 05:12
  • 1
    @javdromero worked, thanks :) – Tomas Loksa May 19 '21 at 05:12

0 Answers0