Last active
July 24, 2018 08:40
-
-
Save SelvinPL/4d00f9a58a9fc8e3dc074601a3e33144 to your computer and use it in GitHub Desktop.
Terrible and stupid idea of passing Activity via Bundle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout 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" | |
android:orientation="vertical" | |
tools:context=".MainActivity"> | |
<TextView | |
android:id="@+id/text1" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Hello World!" /> | |
<FrameLayout | |
android:id="@+id/content" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pl.selvin.terribleidea; | |
import java.io.Serializable; | |
public interface ISomeAction extends Serializable { | |
String KEY = "interface"; | |
void sendAddress(String address); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pl.selvin.terribleidea; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Button; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity implements ISomeAction { | |
TextView textView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
textView = findViewById(R.id.text1); | |
if (savedInstanceState == null) { | |
//pretty normal stuff: WE DO NOT ADD FRAGMENT AGAIN AFTER ROTATION | |
//it will be recreated by system WITH THE SAME ARGS | |
//which obviously means that Activity passed via arguments will not be valid | |
final Fragment fragment = new Frag(); | |
final Bundle args = new Bundle(); | |
//TERRIBLE AND STUPID IDEA DO NOT DO THIS!!! | |
args.putSerializable(ISomeAction.KEY, this); | |
fragment.setArguments(args); | |
getSupportFragmentManager().beginTransaction().add(R.id.content, fragment, "TAG").commit(); | |
} | |
} | |
@Override | |
public void sendAddress(String address) { | |
textView.setText(address); | |
} | |
public static class Frag extends Fragment { | |
@Nullable | |
@Override | |
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
final Button button = new Button(requireContext()); | |
button.setText("Click me!"); | |
button.setOnClickListener(v -> | |
{ | |
Bundle bundle = getArguments(); | |
ISomeAction iSomeAction = (ISomeAction) bundle.getSerializable(ISomeAction.KEY); | |
iSomeAction.sendAddress("aaaaaaa"); | |
//obviously ((ISomeAction)getActivity()).sendAddress("aaaaaaa"); will always work! | |
}); | |
return button; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment