Skip to content

Commit

Permalink
Merge pull request #207 from NordicSemiconductor/bugfix/thread-closed
Browse files Browse the repository at this point in the history
Bugfix: Fixed crash when MainActivity was destroyed when selecting a file
  • Loading branch information
philips77 authored Nov 14, 2024
2 parents d09f3a7 + a71ee77 commit de9f189
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 45 deletions.
17 changes: 11 additions & 6 deletions sample/src/main/java/io/runtime/mcumgr/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.bottomnavigation.BottomNavigationView;

Expand All @@ -39,6 +38,7 @@
import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasAndroidInjector;
import io.runtime.mcumgr.McuMgrTransport;
import io.runtime.mcumgr.sample.application.Dagger2Application;
import io.runtime.mcumgr.sample.databinding.ActivityMainBinding;
import io.runtime.mcumgr.sample.di.Injectable;
Expand All @@ -47,7 +47,6 @@
import io.runtime.mcumgr.sample.fragment.ImageFragment;
import io.runtime.mcumgr.sample.fragment.LogsStatsFragment;
import io.runtime.mcumgr.sample.fragment.ShellFragment;
import io.runtime.mcumgr.sample.viewmodel.MainViewModel;
import io.runtime.mcumgr.sample.viewmodel.mcumgr.McuMgrViewModelFactory;

@SuppressWarnings("ConstantConditions")
Expand All @@ -60,7 +59,7 @@ public class MainActivity extends AppCompatActivity
@Inject
McuMgrViewModelFactory viewModelFactory;
@Inject
BluetoothDevice device;
McuMgrTransport mcuMgrTransport;
@Inject
@Nullable
Uri logSessionUri;
Expand Down Expand Up @@ -115,9 +114,6 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
}
final String deviceAddress = device.getAddress();

new ViewModelProvider(this, viewModelFactory)
.get(MainViewModel.class);

// Configure the view.
final Toolbar toolbar = binding.toolbar;
setSupportActionBar(toolbar);
Expand Down Expand Up @@ -192,4 +188,13 @@ public boolean onOptionsItemSelected(final @NonNull MenuItem item) {
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onDestroy() {
super.onDestroy();

if (isFinishing()) {
mcuMgrTransport.release();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import dagger.Subcomponent;
import io.runtime.mcumgr.sample.viewmodel.FileBrowserViewModel;
import io.runtime.mcumgr.sample.viewmodel.MainViewModel;
import io.runtime.mcumgr.sample.viewmodel.mcumgr.DeviceStatusViewModel;
import io.runtime.mcumgr.sample.viewmodel.mcumgr.EchoViewModel;
import io.runtime.mcumgr.sample.viewmodel.mcumgr.ExecViewModel;
import io.runtime.mcumgr.sample.viewmodel.mcumgr.FilesDownloadViewModel;
import io.runtime.mcumgr.sample.viewmodel.mcumgr.FilesUploadViewModel;
import io.runtime.mcumgr.sample.viewmodel.mcumgr.ImageControlViewModel;
Expand All @@ -20,7 +20,6 @@
import io.runtime.mcumgr.sample.viewmodel.mcumgr.McuMgrViewModel;
import io.runtime.mcumgr.sample.viewmodel.mcumgr.McuMgrViewModelFactory;
import io.runtime.mcumgr.sample.viewmodel.mcumgr.ResetViewModel;
import io.runtime.mcumgr.sample.viewmodel.mcumgr.ExecViewModel;
import io.runtime.mcumgr.sample.viewmodel.mcumgr.StatsViewModel;

/**
Expand All @@ -35,8 +34,6 @@ interface Builder {
McuMgrViewModelSubComponent build();
}

MainViewModel mainViewModel();

DeviceStatusViewModel deviceStatusViewModel();
EchoViewModel echoViewModel();
ResetViewModel resetViewModel();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

package io.runtime.mcumgr.sample.viewmodel.mcumgr;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import io.runtime.mcumgr.sample.di.component.McuMgrViewModelSubComponent;
import io.runtime.mcumgr.sample.viewmodel.FileBrowserViewModel;
import io.runtime.mcumgr.sample.viewmodel.MainViewModel;

public class McuMgrViewModelFactory implements ViewModelProvider.Factory {
private final Map<Class<? extends ViewModel>, Callable<? extends ViewModel>> creators;
Expand All @@ -27,7 +27,6 @@ public McuMgrViewModelFactory(@NonNull final McuMgrViewModelSubComponent viewMod
creators = new HashMap<>();
// we cannot inject view models directly because they won't be bound to the owner's
// view model scope.
creators.put(MainViewModel.class, viewModelSubComponent::mainViewModel);
creators.put(DeviceStatusViewModel.class, viewModelSubComponent::deviceStatusViewModel);
creators.put(EchoViewModel.class, viewModelSubComponent::echoViewModel);
creators.put(ResetViewModel.class, viewModelSubComponent::resetViewModel);
Expand Down

0 comments on commit de9f189

Please sign in to comment.