简体   繁体   中英

Android : Use fragment or change all the view in the activity

I ask you a suggestion for my application. I have to develop one app with this characteristics :

1- only portrait

2- when open the app it shows the bluetooth devices presents

3- when you click on one device the app ask to the user an unlock code and show the connect button

4- after press connect button the app show a loading spinner bar whith two o three buttons

The point is : is It better that I used three different fragment for each behavior or not ?

For now I have did :

one activity for the scan device

one activity for the unlock code

but i don't know where I can put the loading screen (loading spinner bar and the three buttons)

Now I'm thinking of develop in different way. One central activity that handles the loading of 3 different fragments :

  1. one for scan device

  2. one for unlock code and one for loading screen

But I'm new in the Android programming, and I always wonder if I think in correct way or in the wrong way .

And in last : for communicate the chosen device from fragment to the activity I think I will implement a listener in mainactivity. Is it right ?

****EDIT :** *I have another doubt regarding the main question.***

Now after your advice I want develop this app in this way :

  • Main Activity

    • Scan DEvice fragment
    • Unlock Device fragment

    • Loading fragment

  • Started Under Service

In the precedent version I thought to develop three different activity and to use binder and messange to communicate in two direction way to/from the service

Now,instead, there are three different fragments inside the main activity. My question is : for you is better implement the comunication to the service inside mainactivity or inside every single fragment ?

For instance : User selects a device in the scan fragment, this fragment communicates the choise directly to the service or communicates the choise to the mainactivity which forwards the information to the service ?

Thanks for your time :)

Do not use setContentView() to change between states of the application, it can cause inconsistency problems on onBackPressed() - use fragments instead. But you will run into a problem on back press, so you will need to see if there are fragments left in your activity on back press, for which you will need to see https://stackoverflow.com/a/24527530/2413303 ContainerFragment in this question.

    @Override
    public void onCreate(Bundle saveInstanceState)
    {
        super.onCreate(saveInstanceState);
        this.setContentView(R.layout.activity_container);
        if (saveInstanceState == null)
        {               
             getSupportFragmentManager().beginTransaction()
                .add(R.id.activity_container_container, new ExampleFragment())
                .addToBackStack(null)
             .commit();
        }
        getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener()
        {
            public void onBackStackChanged()
            {
                int backCount = getSupportFragmentManager().getBackStackEntryCount();
                if (backCount == 0)
                {
                    finish();
                }
            }
        });
    }

Also, use ButterKnife library, I didn't in this example I linked, even though I should have. It makes the code much less verbose and do the same thing.

public class FancyFragment extends Fragment {
   @InjectView(R.id.button1) Button button1;
   @InjectView(R.id.button2) Button button2;

  @OnClick(R.id.btnSubmit)
  public void submit(View view) {
     // TODO submit data to server...
  }

  @Override 
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.fancy_fragment, container, false);
     ButterKnife.inject(this, view);
     // TODO Use "injected" views...
     return view;
  }
}

And for communication, you can use Otto library as per https://stackoverflow.com/a/28480952/2413303

public class UpdateListEvent {

}

public class MainActivity extends ActionBarActivity {
    ...
    public void updatelist() {
       SingletonBus.INSTANCE.getBus().post(new UpdateListEvent());
    }
}

public class FragmentA extends Fragment {
    @Override
    public void onResume() {
        super.onResume();
        SingletonBus.INSTANCE.getBus().register(this);
    }

    @Override
    public void onPause() {
        SingletonBus.INSTANCE.getBus().unregister(this);
        super.onPause();
    }

    @Subscribe
    public void onUpdateListEvent(UpdateListEvent e) {
         //do things
    }
}

public enum SingletonBus {
    INSTANCE;

    private Bus bus;

    private SingletonBus() {
        this.bus = new Bus(ThreadEnforcer.ANY);
    }

    public Bus getBus() {
        return bus;
    }
}

I have another doubt regarding the main question.

Now I develop this app in this way :

  • Main Activity

    • Scan DEvice fragment
    • Unlock Device fragment

    • Loading fragment

  • Started Under Service

In the precedent version I thought to develop three different activity and to use binder and messange to communicate in two direction way to/from the service

Now,instead, there are three different fragments inside the main activity, for you is better implement the comunication to the service inside mainactivity or inside every single fragment ?

For instance :

User selects a device in the scan fragment, this fragment communicates the choise directly to the service or communicates the choise to the mainactivity which forwards the information to the service ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM